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

cargo run --example should print available examples #2548

Closed
tyoc213 opened this issue Apr 6, 2016 · 9 comments · Fixed by #6505
Closed

cargo run --example should print available examples #2548

tyoc213 opened this issue Apr 6, 2016 · 9 comments · Fixed by #6505
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself.

Comments

@tyoc213
Copy link

tyoc213 commented Apr 6, 2016

if you clone a repo and has examples and you run cargo run --example you only get help for general usage, what about print the actual examples on the examples directory?

$ cargo run --example
Expected argument for flag '--example' but reached end of arguments.

Usage:
    cargo run [options] [--] [<args>...]

If there where one.rs, two.rs and three.rs inside examples folder it could output something like

$ cargo run --example
Expected argument for flag '--example' but reached end of arguments.

Available examples are: one, two and three.

Usage:
    cargo run [options] [--] [<args>...]

Even more, it can autocomplete the name of the example... well that will be only a NTH.

@alexcrichton alexcrichton added the A-diagnostics Area: Error and warning messages generated by Cargo itself. label Apr 6, 2016
@alexcrichton
Copy link
Member

Sounds like a good suggestion to me!

@AndrewJakubowicz
Copy link

Can I work on this @alexcrichton?

@alexcrichton
Copy link
Member

Certainly @SpyR1014! If you have any questions just lemme know

@AndrewJakubowicz
Copy link

AndrewJakubowicz commented Feb 14, 2018

I've poked around the codebase and am unsure where the best place to intercept and update this error is.

Here's a commit with my attempt at handling the error. I haven't implemented the examples path logic as I feel there are other problems.

The error is created by Docopt and I can't find a way to manipulate the error within Docopt. This leaves the error handling to cargo where I've implemented the intercept logic. Currently it's a hideous if statement.

I'm not sure this is a good design, and am not familiar enough to think of another solution.

Pros of the solution:

  • Right next to the error source.
  • Easy to intercept.

Cons:

  • Will require code in the lib.rs to locate the workspace, examples directory and the files.

I would appreciate any pointers before continuing. Thank you in advance for any help! :)

@alexcrichton
Copy link
Member

Hm yeah it's true we don't have a lot of experience right now catching docopt errors and printing something as a result.

I'm unfortunately not sure how we'd implement a fix for this in a "clean" fashion, so failing that I think it may be ok to special case this for now. Basically if arguments like --example are passed and have no value, we could catch that docopt error, remove the arg, attempt to parse again, and if successful we should have enough information to start printing diagnostics, right?

@AndrewJakubowicz
Copy link

Thank you so much for your response Alex. I've implemented a mostly working solution here

I'm unable to figure out how to get the flag_manifest_path field from the docopts object without access to run's Options struct.

Because of this, the error is only updated if the cwd is the workspace root directory. This is better than nothing.

Example output of running on my test file with an examples directory:

Test 1

examples
├── junk.txt
├── sorting.rs
└── suffix.rs

gives:

error: Expected argument for flag '--example' but reached end of arguments.

Available examples are: sorting and suffix

Usage:
    cargo run [options] [--] [<args>...]

Test 2 - Empty examples directory

Output:

error: Expected argument for flag '--example' but reached end of arguments.

There are no examples in the 'examples' directory

Usage:
    cargo run [options] [--] [<args>...]

Test 3 - Multiple examples

examples
├── one.rs
├── three.rs
└── two.rs

gives:

error: Expected argument for flag '--example' but reached end of arguments.

Available examples are: one, three and two

Usage:
    cargo run [options] [--] [<args>...]

@sanmai-NL
Copy link

@AndrewJakubowicz: can you give an update on this?

@richard-uk1
Copy link
Contributor

I've played with implementing this by hooking into the error, but you need the arguments to work out what examples are appropriate. Therefore, I think you need something like clap-rs/clap#1354 to implement this.

@AndrewJakubowicz
Copy link

@sanmai-NL I have not had a chance to look at this for some time or how this might work after the migration to clap.

@matklad left guidance to move forward here: #5062 (comment)

bors added a commit that referenced this issue Jan 7, 2019
--{example,bin,bench,test} with no argument now lists all available targets

```
cargo run --bin
error: "--bin" takes one argument.
Available binaries:
    cargo

error: process didn't exit successfully: `target\debug\cargo.exe run --bin` (exit code: 101)
```

Previous PR: #5062
Closes #2548

Notes:
- `optional_opt` is a weird name, can someone come up with a better one?
- Should I call clap's `usage()` as well when printing the error message?
@bors bors closed this as completed in #6505 Jan 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants