Skip to content
This repository has been archived by the owner on Jul 8, 2021. It is now read-only.

Use Clap for parsing command line arguments #25

Open
yupferris opened this issue Feb 1, 2016 · 1 comment
Open

Use Clap for parsing command line arguments #25

yupferris opened this issue Feb 1, 2016 · 1 comment

Comments

@yupferris
Copy link
Owner

Suggestion from @CryZe .

https://github.com/kbknapp/clap-rs

@Evrey
Copy link

Evrey commented Mar 9, 2016

It's a nice API, but I personally prefer argparse for two reasons:

  1. argparse directly stores the parsed command line arguments where you want them to be. Thus, instead of fiddling with strings, your command line argument handling is always correctly typed.
  2. argparse is way smaller and has no additional dependencies. This reduces compile time, besides having other benefits.

Here's a sample of how it looks in my GBA emulator:

pub struct CmdLineArgs {
    pub rom_file_path: Option<PathBuf>,
    pub log_file_path: PathBuf,
    pub single_disasm_arm: Option<String>, // String because `Parse` expects decimal, I want hex.
    pub single_disasm_thumb: Option<String>,
    pub verbose: bool,
    pub colour: bool,
    pub exit: bool,
}

fn parse_command_line(args: &mut CmdLineArgs) {
    let mut parser = ArgumentParser::new();
    parser.set_description("A GBA emulator written in Rust.");
    parser.add_option(&["-V", "--version"],
                      Print(format!("GBArs v{}", env!("CARGO_PKG_VERSION"))),
                      "Show current version.");
    parser.refer(&mut args.rom_file_path)
          .add_option(&["--rom"], ParseOption, "Path to a ROM file to load.")
          .metavar("PATH");
    parser.refer(&mut args.log_file_path)
          .add_option(&["--log"], Parse, "Custom path for the log file.")
          .metavar("PATH");
    parser.refer(&mut args.single_disasm_arm)
          .add_option(&["--dasm-arm"], StoreOption,
                      "Prints the disassembly of the given ARM state instruction. \
                       The instruction must be a hex number without base, e.g. 01F7344, \
                       in Big Endian format, i.e. the most significant byte is left.")
          .metavar("INST");
    parser.refer(&mut args.single_disasm_thumb)
          .add_option(&["--dasm-thumb"], StoreOption,
                      "Prints the disassembly of the given THUMB state instruction. \
                       The instruction must be a hex number without base, e.g. 01F7, \
                       in Big Endian format, i.e. the most significant byte is left.")
          .metavar("INST");
    parser.refer(&mut args.verbose)
          .add_option(&["-v","--verbose"], StoreTrue, "Log extra messages and information.")
          .add_option(&["-q","--quiet"], StoreFalse, "Log with less messages and information. (default)");
    parser.refer(&mut args.colour)
          .add_option(&["-c","--with-colour"], StoreTrue, "Enable terminal logging with ANSI colour codes. (default)")
          .add_option(&["-k","--without-colour"], StoreFalse, "Disable terminal logging with ANSI colour codes.");
    parser.refer(&mut args.exit)
          .add_option(&["-x","--exit"], StoreTrue, "Exit early after handling the command line arguments.");
    parser.parse_args_or_exit();
}

Edit: Added the (simplified) structure receiving the parsed arguments for clarity.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants