Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

METAVAR, Improved help, Samples (Based on #187) #206

Merged
merged 4 commits into from
Sep 22, 2022

Conversation

p-ranav
Copy link
Owner

@p-ranav p-ranav commented Sep 22, 2022

  • Added metavars. If set, then will be used in help for the value placeholder for options, or the placeholder alone for regular positional arguments.
  • Help option sections are indented by two spaces, and comma-separated. They now show metavar (this means that an option with value now shows as --option VALUE Description of Option instead of --option Description ....
  • Usage section now enumerates options, instead of just [options]. Added ArgumentParser::usage() to retreive this string alone (in case you wanted to, say, print usage if arguments incorrectly passed).
  • Not showing [default: ...] for implicit values. This seemed unusual. It might be better to rework the help system to recognise a placeholder (even python's %(default)s) rather than try to magically get this right.
  • Added nargs to help, right after description of the argument
  • Added samples for testing and demo

Example

#include <argparse/argparse.hpp>

int main(int argc, char *argv[]) {
  argparse::ArgumentParser program("main");
  program.add_argument("thing").help("Thing to use.").metavar("THING");
  program.add_argument("--member")
      .help("The alias for the member to pass to.")
      .metavar("ALIAS");
  program.add_argument("--verbose").default_value(false).implicit_value(true);

  program.add_description("Forward a thing to the next member.");
  program.add_epilog("Possible things include betingalw, chiz, and res.");

  program.parse_args(argc, argv);

  std::cout << program << std::endl;
}
foo@bar:/home/dev/$ ./build/samples/description_epilog_metavar --help
Usage: main [-h] [--member ALIAS] [--verbose] THING

Forward a thing to the next member.

Positional arguments:
  THING         	Thing to use. 

Optional arguments:
  -h, --help    	shows help message and exits 
  -v, --version 	prints version information and exits 
  --member ALIAS	The alias for the member to pass to. 
  --verbose     	

Possible things include betingalw, chiz, and res.

@p-ranav p-ranav self-assigned this Sep 22, 2022
@p-ranav p-ranav merged commit ce0b491 into master Sep 22, 2022
@p-ranav p-ranav deleted the feature/metavar_help_and_usage branch September 22, 2022 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant