GithubHelp home page GithubHelp logo

commander.h's Introduction

commander.h

A basic command line utility for C.

API

/**
 * Returns the number of options created
 */
int cmd_opt_count();
/**
 * Prints a list of options
 */
void cmd_opt_print();
/**
 * Returns the index of where the option is located in argv
 */
int cmd_opt_index();
/**
 * Returns the index of where the value is located in argv.
 * Note: When no value is provided, the index will be 0 which points to argv[0].
 */
int cmd_val_index();
/**
 * Create an option that expects a value.
 */
void cmd_opt_value();
/**
 * Create an option that expects a flag that could have a value.
 */
void cmd_opt(char* flag, char* name, bool valuable);
/**
 * Parse the command arguments.
 * Returns -1 once the process has completed.
 */
int cmd_parse(int argc, char* argv[]);
/**
 * Frees the memory used for commander.
 * Returns 0 when completed.
 */
int cmd_free(); 

Usage

Simply include "commander.h" into your program and compile.

Note: Warnings about write strings may appear when compiling in C++. You can add the -Wno-write-strings option to surpress it.

#include "commander.h"
#include <stdio.h>

int main(int argc, char* argv[]) {
  /* Expect the options in the following order */

  // Expect a value as the first argument
  cmd_opt_value();
  // Expect a flag and a value as the second argument
  cmd_opt("-r", "--row", true);
  cmd_opt("-c", "--column", true);
  // Expect a flag that has no value as the fourth argument
  cmd_opt("-s", "--silent", false);

  int option;
  while ((option = cmd_parse(argc, argv)) != -1) {
    // Switch on the option id
    switch (option) {
      case 1: {
        // Use the cmd_opt_index and cmd_val_index functions to print
        // the index and value of the argument for the first option;
        printf("option index: %d, value: %s\n", cmd_opt_index(), argv[cmd_val_index()]);
        break;
      }
      case 4: {
        printf("option index %d, silent flag: %s\n", cmd_opt_index(), argv[cmd_opt_index()]);
        break;
      }
      default: {
        printf("option index: %d, flag: %s, value: %s\n", cmd_opt_index(), argv[cmd_opt_index()], argv[cmd_val_index()]);
        break;
      }
    }
  }

  // Print the options
  cmd_opt_print();
  
  // Print the number of options
  printf("option count: %d\n", cmd_opt_count());

  // Free the memory used at the end
  return cmd_free();
}

commander.h's People

Contributors

iwatakeshi avatar

Stargazers

 avatar

Watchers

James Cloos avatar  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.