Skip to content

Conversation

@alarsyo
Copy link
Contributor

@alarsyo alarsyo commented Mar 24, 2020

cargo passes all args to the subcommand program, which means our binary will receive "mutagen" as first argument when invoked like this:

 $ cargo mutagen

This produced an error, because structopt doesn't expect a "mutagen" argument at all:

 error: Found argument 'mutagen' which wasn't expected, or isn't
 valid in this context

 USAGE:
     cargo-mutagen [FLAGS] [OPTIONS]

     For more information try --help

For a similar problem, see rust-lang/rustfmt#3569.

This is how they handle it: https://github.com/rust-lang/rustfmt/blob/9124dd88d6ef14d0ae77dc52ff5e9598f24a75a0/rustfmt-core/rustfmt-bin/src/cargo-fmt/main.rs#L77

I've opted for a simpler solution because I don't think mutagen could be one of the arguments for cargo-mutagen currently (naming an eventual feature mutagen doesn't seem right). If you think this might be a problem later on, we can find a better fix :)

let mutagen_start = Instant::now();

let opt = Options::from_args();
// drop mutagen arg in cargo-subcommand mode
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there is another "mutagen" arg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added code to try and detect when we're in "cargo subcommand" mode. From the wiki, cargo sets the CARGO env var to its path on the system. This is assuming that the user hasn't set the CARGO env var himself for some reason :/

cargo passes all args to the subcommand program, which means our binary
will receive "mutagen" as first argument when invoked like this:

     $ cargo mutagen

This produced an error, because structopt doesn't expect a "mutagen"
argument at all:

     error: Found argument 'mutagen' which wasn't expected, or isn't
     valid in this context

     USAGE:
         cargo-mutagen [FLAGS] [OPTIONS]

         For more information try --help
@alarsyo alarsyo force-pushed the cargo-mutagen-subcommand-fix branch from 5e23b68 to 87abab2 Compare March 24, 2020 21:26
@llogiq
Copy link
Owner

llogiq commented Mar 26, 2020

Looks good to me. Thank you!

@llogiq llogiq merged commit f824925 into llogiq:master Mar 26, 2020
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.

2 participants