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

How do I change the names of the values in a clap argument that takes multiple values?

As part of my CLI tool, I have a clap::Arg that takes multiple values, representing an (x, y) coordinate. I want the use the be able to pass in the value as -p/--position 1 0 .arg( clap::Arg::with_name("position") .help("The position for…
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
6
votes
2 answers

Method call on clap::App moving ownership more than once

Even after reading the chapters about reference ownership and borrowing, I cannot understand some things in the following code, effectively stopping me from calling more than one method from clap::App! extern crate clap; use clap::App; fn main()…
Jiehong
  • 786
  • 1
  • 7
  • 16
5
votes
1 answer

How to use clap to take create a vector

I have a basic struct like this pub struct Args { #[clap(short, long, value_parser)] pub files: Vec, } I'm trying to get this struct to take multiple values like such cargo run -- --files hello world But when I run this, it doesn't see…
Zach W
  • 194
  • 11
5
votes
2 answers

clap.rs not printing colors during `--help`

So I migrated from clap v3.x to v4.x. I am not getting the color during the help output as I got it in v3.x. Everything was completely white. I used the basic code from the example (https://github.com/clap-rs/clap/blob/master/examples/git.rs). Below…
TheFishTheySay
  • 190
  • 2
  • 17
5
votes
3 answers

Using clap's #[derive(Parser)], how can I accept a std::time::Duration?

I want to accept a std::time::Duration on a command line. I'm using clap with #[derive(Parser)] to generate the parameter parser. Is there any way I can directly accept an input, rather than accepting a number and doing the conversion…
ddulaney
  • 843
  • 7
  • 19
5
votes
3 answers

rust clap parse ipv4Addr

I want to use the clap derive API in order to parse an Ipv4Addr. #![allow(unused)] use clap; // 3.1.6 use clap::Parser; use std::net::Ipv4Addr; #[derive(Parser, Debug)] #[clap(author, version, about, long_about = None)] struct Args { …
jonathan-dev
  • 330
  • 1
  • 3
  • 16
5
votes
1 answer

Dynamically generating arguments Clap

I'm trying to figure out how to dynamically generating arguments from input arguments with Clap. What I'm trying to emulate with Clap is the following python code: parser = argparse.ArgumentParser() parser.add_argument("-i", type=str,…
5
votes
1 answer

Idiomatic rust way to properly parse Clap ArgMatches

I'm learning rust and trying to make a find like utility (yes another one), im using clap and trying to support command line and config file for the program's parameters(this has nothing to do with the clap yml file). Im trying to parse the commands…
ECO
  • 359
  • 2
  • 13
5
votes
2 answers

How can I prevent the last argument from needing to be quoted with clap?

I'm using clap and I get a unexpected behaviour when trying to parse arguments. My command line tool is supposed to work like this foo -u e.g.: foo -u jack echo s foo -u paul ls -al I need to get options such as user, but the…
Marcelo Boeira
  • 860
  • 8
  • 22
4
votes
0 answers

How do I "flatten" a subcommand with Clap

I have two types of commands in my program, and I want to group one of the types in one enum. I don't want the user to have to type extra though, so I want to expose those commands at top level. How do I "flatten" subcommands? Currently I'm working…
Typhaon
  • 828
  • 8
  • 27
4
votes
1 answer

How to extract config value from env variable with clap derive?

I cannot find in the docs how to set this up. Let's say I have #[derive(Parser, Debug)] pub struct Opts { #[clap(long)] dry_run: bool, } What do I need to do to get dry_run from APP_DRY_RUN env variable?
ditoslav
  • 4,563
  • 10
  • 47
  • 79
4
votes
2 answers

How to make one argument imply another without needing an explicit value? (--foo, not --foo true)

I want one argument to imply another, though they don't take explicit values. --simple-anime or --complex-anime should imply --anime. The API that should work is default_value_ifs, saying that if either of the former is present, --anime will also be…
piojo
  • 6,351
  • 1
  • 26
  • 36
4
votes
1 answer

Use - as stdin/stdout

A lot of command-line tools let you use - for standard input or standard output. Is there an idiomatic way to support that in Rust? It looks like the most common way to handle command-line arguments is clap. If I just want to handle paths and…
Daniel H
  • 7,223
  • 2
  • 26
  • 41
4
votes
2 answers

How to use an internal library Enum for Clap Args

I am currently working on a Rust port of a security tool. Inline with Rust's guides, I want to segregate the core library into its own crate, so that we can create various tools (CLI, API, streams etc.) that interface with with the core library…
Juxhin
  • 5,068
  • 8
  • 29
  • 55
3
votes
0 answers

How to combine ArgAction::Count and value_parser

I'd like to use ArgAction::Count to count the number of occurrences of my --verbose flag, and then send the result through a closure to convert it to a Verbosity enum. At the moment I'm trying this: use clap::{Parser, ArgAction,…
1
2
3
9 10