Skip to content

Instantly share code, notes, and snippets.

@michaelcoyote
Created May 20, 2020 23:44
Show Gist options
  • Save michaelcoyote/9a2aabe9a99cf7b4bb7970f798ec2f1b to your computer and use it in GitHub Desktop.
Save michaelcoyote/9a2aabe9a99cf7b4bb7970f798ec2f1b to your computer and use it in GitHub Desktop.
Rust StructOpt question

StructOpt question

Need to figure out a way to turn mutually exclusive options from StructOpt into someting I can select at runtime. In Python I can do this using argparse and then testing existance on the option something like this:

if args.argument1:
    thing(args.argument1)
else if args.argument2:
    otherthing(args.argument2)

This is less straightforward in rust.

Because Rust is extremely type safe, I can't simply test this like I do in Python. I get the following error when compiling:

michael@smolbook:~/tmp/fossa_admin_tool (develop)$ cargo build
   Compiling fossa_admin_tool v0.1.1 (/Users/michael/tmp/fossa_admin_tool)
error[E0308]: mismatched types
  --> src/main.rs:93:8
   |
93 |     if opts.orgname {
   |        ^^^^^^^^^^^^ expected bool, found struct `std::string::String`
   |
   = note: expected type `bool`
              found type `std::string::String`

error[E0308]: mismatched types
   --> src/main.rs:109:15
    |
109 |     } else if opts.setid {
    |               ^^^^^^^^^^ expected bool, found u32

error: aborting due to 2 previous errors

Tests such as if expect me to use a boolean value. I need a way to test if a option is set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment