qsharp-compiler icon indicating copy to clipboard operation
qsharp-compiler copied to clipboard

Allow setting default values for command-line arguments in standalone Q# projects

Open cgranade opened this issue 4 years ago • 2 comments

Is your feature request related to a problem? Please describe. Currently, when running Q# entry points with inputs at the command line, those inputs are exposed as command-line parameters. It would be nice if we could give default values, however, so that users don't need to provide all of them to explore and get started with applications.

Describe the solution you'd like It would be helpful to be able to provide defaults either as attributes or as csproj project properties.

cgranade avatar May 03 '21 18:05 cgranade

If I interpret correctly you mean in the source code? Why not go with the same setup as for all other languages, which would be to define them in the launch configuration/project settings instead?

bettinaheim avatar May 06 '21 02:05 bettinaheim

I'm fine with either way, though I personally lean more towards attributes in source code, paralleling how it tends to work for a lot of common libraries (examples below). In particular, I like that that keeps the declaration of an input near its default value, but again just my 2¢.

# Python + Click, example via https://click.palletsprojects.com/en/7.x/.
import click

@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
              help='The person to greet.')
def hello(count, name):
    """Simple program that greets NAME for a total of COUNT times."""
    for x in range(count):
        click.echo('Hello %s!' % name)

if __name__ == '__main__':
    hello()
// C# + System.CommandLine, example via https://github.com/dotnet/command-line-api/blob/main/docs/Your-first-app-with-System-CommandLine.md
var rootCommand = new RootCommand
{
    new Option<int>(
        "--int-option",
        getDefaultValue: () => 42,
        description: "An option whose argument is parsed as an int"),
    new Option<bool>(
        "--bool-option",
        "An option whose argument is parsed as a bool"),
    new Option<FileInfo>(
        "--file-option",
        "An option whose argument is parsed as a FileInfo")
};
// Rust + Clap, example via https://docs.rs/clap/2.33.3/clap/struct.Arg.html#method.default_value
let m = App::new("prog")
    .arg(Arg::with_name("opt")
        .long("myopt")
        .default_value("myval"))
    .get_matches_from(vec![
        "prog"
    ]);


assert_eq!(m.value_of("opt"), Some("myval"));
assert!(m.is_present("opt"));
assert_eq!(m.occurrences_of("opt"), 0);

cgranade avatar May 06 '21 03:05 cgranade