Questions tagged [clap]

Clap is a command line argument parser for the Rust programming language.

Clap is a command line argument parser for the Rust programming language.

149 questions
40
votes
3 answers

Is there any straightforward way for Clap to display help when no command is provided?

I'm using the Clap crate for parsing command line parameters. I've defined a subcommand ls that should list files. Clap also defines a help subcommand that displays information about the application and its usage. If no command is provided, nothing…
rpeshkov
  • 4,877
  • 3
  • 27
  • 43
23
votes
4 answers

How do I specify a boolean command line flag using Clap?

I want to add a command line flag. It's a flag, so it does not take an argument, it is either present or not. I also need to know how to read the flag, either TRUE or FALSE. This is the code for defining the flag: .arg( Arg::with_name("metal") …
Peter Prographo
  • 1,141
  • 1
  • 10
  • 27
22
votes
1 answer

Clap options with derive macros fails to compile in rust clap 3.0.0-beta.5

I'm trying out the "Using Derive Macros" example on the index page for the latest beta version of clap: // (Full example with detailed comments in examples/01d_quick_example.rs) // // This example demonstrates clap's full 'custom derive' style of…
Huw Walters
  • 1,888
  • 20
  • 20
20
votes
1 answer

How to make a default subcommand with clap and derive

I am trying to have a subcommand launched if there is no argument that has been supplied by the user and I could not find any way to do this. If there is no subcommand supplied the help will show up when I want some action instead to be passed.
16
votes
1 answer

Taking multiple values in an argument in Clap

I'm using Clap and I'm trying to make it so that a subcommand can take multiple values for the argument. The interface I'm after is: just use repo [files] An example: just use radar/dot-files gitaliases ryan-aliases The repo argument here will be…
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
12
votes
1 answer

Iterate through positional arguments with clap

Say I have the following command line: ./app foo bar baz I want to get this array out of it: ["foo", "bar", "baz"] Is there a way to do this in clap, given that the positional arguments can be of arbitrary count?
tshepang
  • 12,111
  • 21
  • 91
  • 136
9
votes
1 answer

Provide Default Value for ArgEnum in Rust Clap CLI

In Clap, we can use enum as the input for the cli according to https://docs.rs/clap/latest/clap/trait.ValueEnum.html: #[derive(clap::Parser)] struct Args { #[clap(value_enum)] level: Level, } #[derive(clap::ValueEnum, Clone)] enum Level { …
Yuchen
  • 30,852
  • 26
  • 164
  • 234
7
votes
2 answers

How to get clap to process a single argument with multiple values without having to specify the flag multiple times?

I would like to have a command such that do_something --list 1 2 3 would result in the field in the struct being set to [1, 2, 3]. The following code works for do_something --list 1 --list 2 --list 3: use clap::Parser; // 3.2.8 #[derive(Parser,…
Marvin Mednick
  • 179
  • 1
  • 7
7
votes
2 answers

ArgMatches' get_one cannot downcast f64

I have used clap crate to parsing arguments in my code. Minimal structure of my code about defining and parsing arguments are as follow. use clap::builder::Command; use clap::{Arg, ArgMatches}; let matches = Command::new("test") …
StatPhy
  • 121
  • 4
7
votes
1 answer

clap default value - how to use it

Playing with Rusts clap crate for the first time. And I wonder how to use the default value of a command line option, when the option was not specified at the command line. Given I specified a default_value in the yaml file (see snippet below), I…
BitTickler
  • 10,905
  • 5
  • 32
  • 53
7
votes
2 answers

How to attach possible_values to a struct with structopt?

clap allows you to provide list of accepted values using possible_values like this. let mode_vals = ["fast", "slow"]; .possible_values(&mode_vals) How to do this with structopt?
eonil
  • 83,476
  • 81
  • 317
  • 516
7
votes
3 answers

Is there a way to get clap to use default values from a file?

I'm programming a CLI using clap to parse my arguments. I want to provide defaults for options, but if there's a config file, the config file should win against defaults. It's easy to prioritize command line arguments over defaults, but I want the…
picked name
  • 309
  • 1
  • 3
  • 9
6
votes
0 answers

How to test CLI arguments with clap in Rust?

What would be the most idiomatic way to write tests for a CLI program using clap? I'm currently doing it like this: #[derive(Debug, Parser)] #[clap(author, version, about)] pub struct StructArgs { #[clap(subcommand)] pub command_type:…
rdxdkr
  • 839
  • 1
  • 12
  • 22
6
votes
1 answer

clap capture all remaining arguments in one field in Derive API?

I am using clap v3.1.18 with following code. #[derive(Parser, Debug)] #[clap(author, version, about = ABOUT_TEXT, long_about = Some(ABOUT_TEXT))] struct Args { /// The root folder on local filesystem to scan #[clap(short, long,…
Mr.Wang from Next Door
  • 13,670
  • 12
  • 64
  • 97
6
votes
3 answers

Parse user input String with clap for command line programming

I'd like to create a command line that utilizes clap to parse input. The best I can come up with is a loop that asks the user for input, breaks it up with a regex and builds a Vec which it somehow passes to loop { // Print command prompt and get…
Will Nilges
  • 160
  • 3
  • 12
1
2 3
9 10