Giter VIP home page Giter VIP logo

simple-cli's Introduction

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;
}

simple-cli's People

Contributors

kwadrat avatar vpetrigo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.