Questions tagged [structopt]

related to Rust crate StructOpt

related to Rust crate StructOpt which is used for structured command line arguments

30 questions
15
votes
2 answers

How to use an enum that represents subcommands with StructOpt?

Referring to the "Git" example of StructOpt, I do not understand how I am then supposed to use the data from the arguments. I am fairly new to Rust so I am guessing it is obvious. Unfortunately, all examples I can find with an enum only do a…
Dante
  • 757
  • 1
  • 5
  • 12
12
votes
4 answers

How can I use enums in structopt?

I'd like to make StructOpt work with enums such that every time a user passes -d sunday it'd parsed as a Day::Sunday: #[macro_use] extern crate structopt; use std::path::PathBuf; use structopt::StructOpt; // My enum enum Day { Sunday,…
H. Desane
  • 593
  • 1
  • 8
  • 16
11
votes
2 answers

How to make an argument optional based on the presence of another one in structopt?

I have a command line tool that has two possible arguments: --version (which will print out the version number and quit) --out (which is the path to some output file into which magic is going to be poured). If a user passes --version I do not care…
kmp
  • 10,535
  • 11
  • 75
  • 125
9
votes
1 answer

How to use StructOpt to parse an argument into a Vec without it being treated as multiple arguments?

I have this code: #[derive(StructOpt)] pub struct Opt { /// Data stream to send to the device #[structopt(help = "Data to send", parse(try_from_str = "parse_hex"))] data: Vec, } fn parse_hex(s: &str) -> Result { …
Geob-o-matic
  • 5,940
  • 4
  • 35
  • 41
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
4
votes
1 answer

StructOpt: how to combine all arguments in a single string?

What I want to do is to access user-provided command-line arguments as a single string. First, I did this as: let lst: Vec = std::env::args().collect(); let res: String = lst[1..].join(" "); But then I decided to try doing it with structopt…
Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72
3
votes
1 answer

Unable to provide CLI arguments to `cargo test`

I used to be able to run specific, named tests from the command-line interface like this: cargo test . But now this gives me the error running 1 test error: Found argument '' which wasn't expected, or isn't valid in this…
Thorkil Værge
  • 2,727
  • 5
  • 32
  • 48
3
votes
1 answer

Getting relative order of different command line options using clap & structopt

The Problem I have a command that takes different options and the relative order of those options is important to the semantics of the command. For example, in command --config A --some-option --config-file B --random-option --config C…
milend
  • 61
  • 4
3
votes
0 answers

structops - how to make argument optional based on boolean flag

I have program that accepts 2 boolean flags -d for decode and -e for encode. However, if -e is specified, additional message string needs to be provided (message to encode). This string should not be present if -d is specified. How can I accomplish…
Ach113
  • 1,775
  • 3
  • 18
  • 40
3
votes
0 answers

Is there a way to change OUT_DIR for a build.rs

Currently I'm trying to write a build.rs that creates a bunch of autocompletion scripts for a cli app. // build.rs fn main() { let outdir = match std::env::var_os("OUT_DIR") { None => return, Some(outdir) => outdir, }; …
3
votes
1 answer

How can I access a runtime-defined variable in stuctopt definitions?

I would like to be able to use the value of a variable (or better yet, the return of a function(arg)) as the about string for a CLI program defined using structopt. The end goal is a fully localized CLI that detects the system language or an ENV var…
Caleb
  • 5,084
  • 1
  • 46
  • 65
3
votes
1 answer

How can I pass version information from Vergen to StructOpt?

I'm trying to setup a simple CLI program in Rust, but I want it to display the version based on the info in git describe rather than whatever is it the Cargo.toml file. I setup some basic dependencies: [dependencies] structopt =…
Caleb
  • 5,084
  • 1
  • 46
  • 65
3
votes
2 answers

How to use a CLI argument after subcommand with the structopt crate?

For example, running my application with ./app --foo=bar get works well, but ./app get --foo=bar Produces an error: error: Found argument '--foo' which wasn't expected, or isn't valid in this context USAGE: app --foo get Code: use…
NikBond
  • 743
  • 1
  • 5
  • 20
2
votes
1 answer

How to create nested subcommands with structopt?

Introduction I currently have subcommands working using structopt, similarly to this other answer. However I would like to have nested subcommands similarly of how docker works: docker image ls docker image pull Current Code main.rs mod cli; use…
itasahobby
  • 301
  • 4
  • 15
2
votes
2 answers

StructOpt - how to provide a default value for a Vec?

I'm looking for a way to initialize a structopt Vec field with multiple items by default. I can do it for a single item with: use structopt::StructOpt; #[derive(Debug, StructOpt)] pub struct Cli { #[structopt(default_value = "foo")] foo:…
imbolc
  • 1,620
  • 1
  • 19
  • 32
1
2