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
3
votes
1 answer

Using multiple values of an argument in Clap

I am using Clap and my YAML file has the following: args: - DIRECTORY help: one or more directories required: true multiple: true In my main.rs, I want to get the name of each of the directory passed as an argument and do something like…
ka s
  • 73
  • 1
  • 7
2
votes
1 answer

Clap - default value for PathBuf

How can I set the default value for clap PathBuf argument? Is it even doable? I keep bumping from one error to another. When I try doing it the "Struct" way: use std::{env, path::PathBuf}; use clap::{Parser, Args}; fn get_default_log_path() ->…
Elembivios
  • 113
  • 10
2
votes
0 answers

How to create a positional argument with "local" option with clap?

I am trying to create something like this with Clap: cli-test --color "red" "rose" --color "yellow" "tulip" ... I.e., a positional argument (the flower) which can occur multiple times with an assigned/"local" option (the color). Is this possible…
mipi
  • 21
  • 2
2
votes
1 answer

Setting in boolean flag in Rust CLI tool using `clap`

I am trying to have a CLI tool that redacts files in a folder according to some specified regexes. In debugging, as an example: cargo run -- folder ./tests/test_files -t emails ip -r which means to redact all files in folder path =…
Jim
  • 450
  • 2
  • 10
2
votes
1 answer

Set default value for argument of type Vec using Clap 4 Rust

In Clap 2, the following: .arg( Arg::with_name("files") .value_name("FILE") .help("Input file(s)") .multiple(true) .default_value("-"), ) will produce: USAGE: catr [FLAGS] [FILE]... FLAGS: -h, --help Prints…
DavidS
  • 2,179
  • 4
  • 26
  • 44
2
votes
1 answer

Clap - subcommands with possibly shared sets of default values?

I've been going in circles on this for a while and haven't found a good solution: I've got a bunch of simulations in the same codebase. I'm trying to be an adult and use command line arguments to pick which simulation runs and with what parameters.…
Edward Peters
  • 3,623
  • 2
  • 16
  • 39
2
votes
1 answer

argument validation in clap v4

I am using crate clap v4。When I try to write something validating arguments against regex, I had some problem with lifetimes. Document of ValueParser for convenience My code as following: pub fn validator_regex(r: &'static str) -> ValueParser { …
ooabc
  • 33
  • 4
2
votes
1 answer

Where are clap macros coming from if they are not being imported?

In the following code snippet, I'm not importing the macros clap and arg but they're still available to the code. Where are they coming from? use clap::Parser; #[derive(Parser, Debug)] struct Arguments { #[clap(short, long)] first_part:…
Danilo Souza Morães
  • 1,481
  • 13
  • 18
2
votes
1 answer

How do I mock user input in the clap library for Rust?

I am currently building a Rust CLI with clap. Does anyone have any pointers as to how I can mock/mimic user input so I can write my tests?
KamiWar
  • 41
  • 5
2
votes
0 answers

Using clap-derive with two groups of arguments

Given this Clap argument struct, I would like to allow users either to supply the config param, or any of the other params that are being flattened from the sub-structs. The connection param would then be required, but other flattened params are…
Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97
2
votes
0 answers

How to parse command line argument to non-unit enum with clap?

I have this enum: #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)] pub enum TheAge { Adult, Age(u8) } And the cli struct #[derive(Parser)] #[command(author, version, about, long_about)] pub struct Cli { …
Finlay Weber
  • 2,989
  • 3
  • 17
  • 37
2
votes
1 answer

How to parse custom string with Clap derive

I found a link for what i want here: Parse user input String with clap for command line programming But it's not completely clear. I see lots of post using App::new() but i can't find any trace of it in clap documentation. I want to parse a string…
miyoku
  • 107
  • 1
  • 13
2
votes
1 answer

Using a structure as a command line argument in clap

Trying to use a struct within a struct in clap: use clap::{Args, Parser}; use std::path::PathBuf; #[derive(Parser, Debug)] enum Command { Foo(Foo), } #[derive(Args, Debug)] struct Foo { bar: Option, path:…
montekki
  • 93
  • 3
2
votes
2 answers

enums in nim for wrapper of c library

I am trying to create a simple nim wrapper around the Clever Audio Plugin c library. In c there is an enum of format flags that can be activated using bitwise operations. summary of the c code # definitions enum clap_note_dialect { …
Alex Gustafson
  • 198
  • 1
  • 8
2
votes
2 answers

Accept optional file on command line, default to stdin

I'm building a CLI program that takes as an optional final argument the name of a file to read from, which may be left off to read from standard input instead, as with cat and similar UNIX programs. Is there any way I could have clap populate a…
Mark Reed
  • 91,912
  • 16
  • 138
  • 175