rustendo64 icon indicating copy to clipboard operation
rustendo64 copied to clipboard

Use Clap for parsing command line arguments

Open yupferris opened this issue 9 years ago • 1 comments

Suggestion from @CryZe .

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

yupferris avatar Feb 01 '16 11:02 yupferris

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.

Evrey avatar Mar 09 '16 10:03 Evrey