Skip to content

Latest commit

History

84 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

CI

Cmdargs

Command-line and config files parsing single header-file library for C++17.

Only long option names is supported.

The idea

Cmdargs this is an attempt to implement the concept of if the code is successfully compiled - it works correctly and no room for error and zero boilerplate code.

Capabilities

  • default values for options
  • optional and required options.
  • option relations: and, or, not.
  • predefined converters for std::vector/std::list/std::set/std::map

Command line example

// using the whole group// declaring key-wordsstruct: cmdargs::kwords_group {
CMDARGS_OPTION(fname, std::string, "source file name")
CMDARGS_OPTION(fsize, std::size_t, "source file size", optional)
} const kwords;
intmain(int argc, char **argv) {
std::string emsg;
auto args = cmdargs::parse_args(&emsg, argc, argv, kwords);
if ( !emsg.empty() ) {
std::cout << "cmdline parse error: " << emsg << std::endl;
returnEXIT_FAILURE;
}
constauto fname = args.get(kwords.fname);
assert(args.is_set(kwords.fsize));
constauto fsize = args.get(kwords.fsize);
}
// using structure bindings// declaring key-wordsstruct: cmdargs::kwords_group {
CMDARGS_OPTION(fname, std::string, "source file name")
CMDARGS_OPTION(fsize, std::size_t, "source file size", optional)
} const kwords;
intmain(int argc, char **argv) {
std::string emsg;
// 'fname' - std::optional<std::string>// 'fsize' - std::optional<std::size_t>constauto [fname, fsize] = cmdargs::parse_args(&emsg, argc, argv, kwords).optionals(); // or `.values()` can be usedif ( !emsg.empty() ) {
std::cout << "cmdline parse error: " << emsg << std::endl;
returnEXIT_FAILURE;
}
std::cout << "fname: is set=" << static_cast<bool>(fname) << ", v=" << fname.value() << std::endl;
if ( fsize ) {
std::cout << "fsize: is set=true, v=" << fsize.value() << std::endl;
} else {
std::cout << "fsize: is set=false, v=<UNINITIALIZED>" << std::endl;
}
}
// using part of the options// declaring key-wordsstruct: cmdargs::kwords_group {
CMDARGS_OPTION(fname, std::string, "source file name")
CMDARGS_OPTION(fsize, std::size_t, "source file size", optional)
CMDARGS_OPTION(fmode, std::string, "processing mode")
} const kwords;
intmain(int argc, char **argv) {
std::string emsg;
auto args = cmdargs::parse_args(&emsg, argc, argv, kwords.fname, kwords.fmode);
if ( !emsg.empty() ) {
std::cout << "cmdline parse error: " << emsg << std::endl;
returnEXIT_FAILURE;
}
constauto fname = args.get(kwords.fname);
constauto fmode = args.get(kwords.fmode);
}

Config-file example

// declaring key-wordsstruct: cmdargs::kwords_group {
CMDARGS_OPTION(fname, std::string, "source file name")
CMDARGS_OPTION(fsize, std::size_t, "source file size", optional)
} const kwords;
intmain(int argc, char **argv) {
std::ifstream is;
std::string emsg;
auto set = cmdargs::from_file(&emsg, is, kwords.fname, kwords.fsize);
if ( !emsg.empty() ) {
std::cout << "file parse error: " << emsg << std::endl;
returnEXIT_FAILURE;
}
constauto fname = args.get(kwords.fname);
constauto fsize = args.get(kwords.fsize);
}

Show help message

// for all the keywords// declaring key-wordsstruct: cmdargs::kwords_group {
CMDARGS_OPTION(fname, std::string, "source file name")
CMDARGS_OPTION(fsize, std::size_t, "source file size", optional)
} const kwords;
intmain(int argc, char **argv) {
cmdargs::show_help(std::cout, argv[0], kwords);
}
// for selected keywords only// declaring key-wordsstruct: cmdargs::kwords_group {
CMDARGS_OPTION(fname, std::string, "source file name")
CMDARGS_OPTION(fsize, std::size_t, "source file size", optional)
} const kwords;
intmain(int argc, char **argv) {
std::string emsg;
auto args = cmdargs::parse_args(&emsg, argc, argv, kwords.fname, kwords.fsize);
cmdargs::show_help(std::cout, argv[0], args);
}

TODO

  • to implement check for child options

About

C++17 library to work with command-line args and config files

Topics

Resources

Stars

6 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages