Skip to content
/ scmdp Public

A cross-platform command line parser library with a user-friendly interface for C

License

Notifications You must be signed in to change notification settings

drxvmrz/scmdp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

scmdp - simplest command-line parser for C

A cross-platform command line parser library with a user-friendly interface

Features

  • Output and error messages formatting easily accessible for changes
  • Non-optional, optional and valuable optional arguments
  • Easily addition of new arguments to your program
  • Autoassembly help for all added arguments
  • Based on cross-platform C standard libraries
  • Automatic memory releasing

Quick start

1. Add library to your own project

The scmdp library consists of only two files: scmdp.h as interface and scmdp.с with implementation.
There is nothing to pre-compile or pre-build, just add them to other project source files.

That's all, You are ready to create your own CLI user interface!

2. Create the workspace

The first thing to do is create a workspace. It stores all data about arguments of the program

ScmdpWorkspace AddWorkspace(char *syntaxHelp, char *description)
Parameters and example

syntaxHelp - String literal with syntax help. It issues if a number of entered parameters is too large or too small
description - String literal describing what the program will do and what its purpose is

ScmdpWorkspace workspace = AddWorkspace("app [-opt] arg1 arg2", "Test command-line application!")

3. Start adding arguments

After creating the workspace, you can safely add new arguments to it.
You can start by adding an argument calls help

void AddHelpArg(ScmdpWorkspace *workspace, char *longKey, char *shortKey)
Parameters and example

workspace - A pointer to created workspace
longKey - The long key to call a help argument (usually prefixed with '--' in UNIX)
shortKey - The short key to call a help argument (usually prefixed with '-' in UNIX)

AddHelpArg(&workspace, "--help", "-h");

Also there's no obstacles to add a non-optional (necessary) agrument

void AddNonOptArg(ScmdpWorkspace *workspace, char *key, char *help, char **valPlace)
Parameters and example

workspace - A pointer to created workspace
key - The name of non-optional argument
help - String literal describing what is the agrument purpose
valPlace - A pointer of variable to drop value of argument

char *arg1;

AddNonOptArg(&workspace, "ARG1", "Necessary argument 1", **arg1);

You can add optional arguments if you need it.
They can be either with or without a value, everything is done by one function

void AddOptArg(ScmdpWorkspace *workspace, char *longKey, char *shortKey, char *help, char *valName, char **valPlace);
Parameters and example

workspace - A pointer to created workspace
longKey - The long key to call a help argument (usually prefixed with '--' in UNIX)
shortKey - The short key to call a help argument (usually prefixed with '-' in UNIX)
help - String literal describing what is the agrument purpose
valName - String literal contains a name of value. Use "" if an argument is a non-valuable.
valPlace - A pointer of variable to drop value of argument

char *optArgWithValue;
char *optArgWithoutValue;

/* With value */
AddOptArg(&workspace, "--option1", "-op1", "Optional argument 1 with value", "VALUE", &optArgWithValue);

/* Without value. It drops a string literal "true" in *optArgWithoutValue if an argument is recieved */
AddOptArg(&workspace, "--option2", "-op2", "Optional argument 2 without value", "", &optArgWithoutValue);

4. Parse you arguments

It is obvious that you were creating arguments to get their value. To do this, use the function

bool ParseArgs(&workspace, argc, argv);
Parameters and example

workspace - A pointer to created workspace
argc - The same is argc in int main(int argc, char* argv[])
argv - The same is argv[] in int main(int argc, char* argv[])

The function returns TRUE if parsing is a success.
Otherwise it returns FALSE.

  /* If the parsing fails, then clear the memory and exit the program */
  if (!ParseArgs(&workspace, argc, argv)) return 0;

5. Enjoy and write your program body!

Examples

Examples folder contains two examples of using the scmdp library. Although they are rather primitive,
they display all the necessary functionality and can serve as hint for a quick start.

Abstract example

A simple application demonstrates using of optional and non-optional arguments.
As a result of the work the string values of the received arguments are displayed in the console.

Calculator

A primitive calculator capable of performing simple operations (*, /, +, -) on integers.
The operation result is displayed in the console.

How to build them?

You can build example programs by three methods depending on the toolset you prefer to use

(UNIX) GCC

Make sure the path to the gcc compiler is in the PATH variable
Then open Terminal in example source files folder /src

gcc *.c -I ../inc -o app
./app

Then enjoy the example! :)


(WINDOWS) MSVC (cl.exe)

Open Developer command prompt and invoke next commands line by line

cd "the way to folder with example sources. example: C:\Users\User\Desktop\scmdp\examples\abstract\src"
cl *.c /I ../inc /Feapp.exe
app

Then enjoy the example! :)


(Cross-platform) CMake

Open Terminal in root folder of example you need to build
or invoke command cd <path to folder> and then next commands

mkdir build
cd ./build
cmake ../
cmake --build ./
./app (or without ./ on WINDOWS)

Then enjoy the example! :)


About

A cross-platform command line parser library with a user-friendly interface for C

Topics

Resources

License

Stars

Watchers

Forks

Languages