Skip to content

Interactive prompts #1471

@bedax

Description

@bedax

Hello, I have a small command line program which I always forget the options for and have to use --help for every time. After looking at darcs recently it occurred to me that it would be nice to get a series of prompts when clap can't parse the input.

For example, let's say I'm writing a simple log program, with the following usage:

USAGE:
   log [OPTIONS] <message>


FLAGS:
   -o, --overwrite
      Overwrite the output file before writing


OPTIONS:
   -p, --path <path>
      The path to the logs file, defaults to `~/.logs`
      
   -l, --level <level>
      The record's log level


ARGS:
   <message> 
      The message to log

If I type log with no arguments, it would be good to be given something like the following:

USAGE: log [OPTIONS] <message>

[y/N] Overwrite the output file before writing
> y

[optional] The path to the log file, defaults to `~/.logs` 
> ~/.some-other-file

[optional] The record's log level 
> info

[required] The message to log 
> something happened

I realise this whole feature isn't necessarily something you want baked into the core, perhaps an external crate, perhaps a feature. Either way, I'm happy to write this, but I'm not sure how best to tap into the parser.

I think it would be useful if clap::App had a get_matches_from_fns function with a signature like this

struct CommandDescription<'a> {
   name: &'a str,
   help: &'a str
}

struct ArgumentDescription<'a> {
   short: &'a str,
   long: &'a str,
   help: &'a str
}

impl clap::App {
   fn get_matches_from_fns<T: Into<OsString> + Clone>
                          (command_getter: impl Fn(CommandDescription) -> T,
                           flag_getter: impl Fn(ArgumentDescription) -> bool,
                           option_getter: impl Fn(ArgumentDescription) -> Option<T>,
                           argument_getter: impl Fn(ArgumentDescription) -> T) 
                           -> ArgMatches ...
}

Then those getters would in this case prompt the user for the input

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions