Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

PCI-BDO/simple-cli

Open more actions menu
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple-cli

Simple command line interface implementation that requires only one array for storing start/end indexes of command/arguments and one variable to store arguments number.

Example

// cli_func.c
#include "cli_func.h"
#include "cli_help.h"
#include "cli_print.h"

const CLI_Func_t cli_functions[] = {{"help", cli_help, "print help message"},
                                    {"print", cli_print, "print arguments that were passed"}
                                    {NULL, NULL, NULL}};
                                    
// main.c
#include "cli_interpreter.h"

#include <stdio.h>

int main() 
{
  const char line[] = " print 1 2 3";
  // maximum number of indexes that might be stored
  const size_t cmd_indexes_size = 8;
  // buffer for storing start/end indexes of a command/arguments
  uint16_t cmd_indexes[cmd_indexes_size];
  // number of arguments
  size_t cmd_len = 0;
  // run CLI interpreter on @line string
  // it also execute cli_print() function with arguments "1", "2", "3"
  cli_interpreter(line, strlen(line), cmd_indexes, &cmd_len);
  // show how much arguments were parsed. Expected: 4 ("print" + "1" + "2" + "3")
  printf("command with args size: %" PRIu64 "\n", cmd_len);
  
  // debug output that shows parsed tokens
  for (size_t i = 0; i < cmd_len * 2; i += 2) {
    printf("CMD start: %d, end: %d\n", cmd_indexes[i], cmd_indexes[i + 1]);

    for (size_t j = cmd_indexes[i]; j < cmd_indexes[i + 1]; ++j) {
      putchar(line[j]);
    }

    putchar('\n');
  }

  return 0;
}

About

Simple command line interface implementation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 93.8%
  • Makefile 6.2%
Morty Proxy This is a proxified and sanitized view of the page, visit original site.