PIP 5.6.1
Platform-Independent Primitives
Public Member Functions | List of all members
PICLI Class Reference

Command-line argument parser class The PICLI class provides convenient parsing of command-line arguments. It supports short keys (e.g., -c), full keys (e.g., –console), and arguments with/without values. More...

#include <picli.h>

Public Member Functions

 PICLI (int argc, char *argv[])
 Constructs PICLI from "argc" and "argv" from "int main()" method.
 
void addArgument (const PIString &name, bool value=false)
 Add argument with name "name", short key = name first letter and full key = name.
 
void addArgument (const PIString &name, const PIChar &shortKey, bool value=false)
 Add argument with name "name", short key = "shortKey" and full key = name.
 
void addArgument (const PIString &name, const char *shortKey, bool value=false)
 Add argument with name "name", short key = "shortKey" and full key = name.
 
void addArgument (const PIString &name, const PIChar &shortKey, const PIString &fullKey, bool value=false)
 Add argument with name "name", short key = "shortKey" and full key = "fullKey".
 
void addArgument (const PIString &name, const char *shortKey, const PIString &fullKey, bool value=false)
 Add argument with name "name", short key = "shortKey" and full key = "fullKey".
 
PIString rawArgument (int index)
 Returns unparsed command-line argument by index "index". Index 0 is program execute command.
 
PIString mandatoryArgument (int index)
 Returns mandatory positional argument by index.
 
PIString optionalArgument (int index)
 Returns optional positional argument by index.
 
const PIStringListrawArguments ()
 Returns unparsed command-line arguments.
 
const PIStringListmandatoryArguments ()
 Returns all mandatory positional arguments.
 
const PIStringListoptionalArguments ()
 Returns all optional positional arguments.
 
PIString programCommand ()
 Returns program execute command without arguments.
 
bool hasArgument (const PIString &name)
 Returns if argument "name" found.
 
PIString argumentValue (const PIString &name)
 Returns argument "name" value, or empty string if this is no value.
 
PIString argumentShortKey (const PIString &name)
 Returns short key of argument "name", or empty string if this is no argument.
 
PIString argumentFullKey (const PIString &name)
 Returns full key of argument "name", or empty string if this is no argument.
 
const PIStringshortKeyPrefix () const
 Returns prefix used for short keys.
 
const PIStringfullKeyPrefix () const
 Returns prefix used for full keys.
 
int mandatoryArgumentsCount () const
 Returns expected count of mandatory positional arguments.
 
int optionalArgumentsCount () const
 Returns expected count of optional positional arguments.
 
void setShortKeyPrefix (const PIString &prefix)
 Sets prefix used for short keys such as "-d".
 
void setFullKeyPrefix (const PIString &prefix)
 Sets prefix used for full keys such as "--debug".
 
void setMandatoryArgumentsCount (const int count)
 Sets count of mandatory positional arguments collected before optional ones.
 
void setOptionalArgumentsCount (const int count)
 Sets count of optional positional arguments. Negative value means unlimited.
 
bool debug () const
 Returns debug mode flag.
 
void setDebug (bool debug)
 Enables or disables debug mode.
 
PIConstChars className () const
 Returns class name.
 
PIString name () const
 Returns human-readable object name.
 

Detailed Description

Command-line argument parser class The PICLI class provides convenient parsing of command-line arguments. It supports short keys (e.g., -c), full keys (e.g., –console), and arguments with/without values.

Synopsis

This class provide handy parsing of command-line arguments. First you should add arguments to PICLI with function addArgument(). Then you can check if there is some argument in application command-line with function hasArgument(), or obtain argument value by argumentValue().

Example

int main(int argc, char ** argv) {
PICLI cli(argc, argv);
cli.addArgument("console");
cli.addArgument("debug");
cli.addArgument("Value", "v", "value", true);
if (cli.hasArgument("console"))
piCout << "console active";
if (cli.hasArgument("debug"))
piCout << "debug active";
piCout << "Value =" << cli.argumentValue("Value");
return 0;
}
Command-line argument parser class The PICLI class provides convenient parsing of command-line argume...
Definition: picli.h:40
#define piCout
Definition: picout.h:36

These executions are similar:

a.out -cd -v 10
a.out --value 10 -dc
a.out -c -v 10 -d
a.out --console -d -v 10
a.out --debug -c --value 10
bool debug() const
Returns debug mode flag.
Definition: picli.h:146