Skip to content

ethanuppal/libcmdapp2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

libcmdapp aims to be a simple and intuitive way to parse command line options and arguments. It aims to provide flexible behavior with the least verbosity possible, intelligently filling any gaps. It is a complete replacement and successor to libcmdapp, which I wrote over 2 years ago.

Item Location
Documentation cmdapp.h
Repository https://github.com/ethanuppal/libcmdapp2
Contents Overview
Features
Install
Example

Note

The latest version of this document can be found here

Features

This library supports

  • Intuitive and succinct interface
    ca_author("My Name");
    ca_year(2024);
    ca_version(1, 0, 0);
  • Long and short options
    const char* expr = "default";
    ca_opt('e', "expr", ".EXPR", &expr, "evaluates an expression");
  • Automatic --help and --version generation, ca_print_version(), ca_print_help()
    Options:
     -e, --expr[=EXPR]  evaluates an expression
    
    • The default implementation integrates with help2man for automatic man pages
    • You can override with ca_override_help_version()
  • Error handling and option conflicts

You can read more about supplying options here.

Install

Unix-like (MacOS, Linux)

The following bash commands will install the library at /usr/local/lib/ and the header files at /usr/local/include/cmdapp/.

git clone https://github.com/ethanuppal/libcmdapp2.git
cd libcmdapp2
sudo make install

You can uninstall by running

sudo make uninstall

from the same directory.

Windows

The following bash commands will build the static and dynamic libraries in the libcmdapp2/ directory.

git clone https://github.com/ethanuppal/libcmdapp2.git
cd libcmdapp2
make all

Example

We'll first initialize the library.

if (ca_init(argc, argv) != 0) {
    perror("ca_init");
    return 1;
}

Then, we can tell it information about our program, such as the year it was written, the authors, etc.

ca_description("Serves as a useful example program for libcmdapp.");
ca_author("First Author");
ca_author("Other Author");
ca_year(2024);
ca_version(1, 0, 0);
ca_versioning_info("All rights reserved.");

We can also tell it how the program should be used in a series of synposes.

ca_synopsis("subcommand [OPTION]...");
ca_synopsis("[OPTION]... FILE");

Finally, we supply the program options

// options without arguments
ca_opt('a', "alert", "", NULL, "oh no!");
ca_opt('b', "very-long-name", "", NULL,
    "this text has been put down a line");

// options with arguments
const char* expr = "EXPR";
ca_opt('e', "expr", ". !@f", &expr, "evaluates an expression");

const char* filename = "FILE";
ca_opt('f', "file", ". !@e", &filename, "processes a file");

// help & version
ca_opt('h', "help", "<h", NULL, "prints this info");
ca_opt('v', "version", "<v", NULL, "prints version info");

Note that ca_opt() and ca_long_opt() can return nonzero on error. However, if you don't make any errors in how you write the functions, you shouldn't ever need to check because they usually won't be dependent on dynamic data.

All you need now is to call ca_parse() or related functions!