This crate defines the StructOpt trait and its custom derive.

If you want to disable all the clap features (colors, suggestions, ..) add default-features = false to the structopt dependency:

[dependencies] structopt = { version = "0.3", default-features = false }

Support for paw (the Command line argument paw-rser abstraction for main ) is disabled by default, but can be enabled in the structopt dependency with the feature paw :

[dependencies] structopt = { version = "0.3", features = [ "paw" ] } paw = "1.0"

First, let's look at the example:

use std :: path :: PathBuf ; use structopt :: StructOpt ; #[ derive ( Debug , StructOpt )] #[ structopt ( name = "example" , about = "An example of StructOpt usage." )] struct Opt { #[ structopt ( short , long )] debug : bool , #[ structopt ( short = "v" , long = "velocity" , default_value = "42" )] speed : f64 , #[ structopt ( parse ( from_os_str ))] input : PathBuf , #[ structopt ( parse ( from_os_str ))] output : Option < PathBuf > , #[ structopt ( short )] out_type : String , #[ structopt ( name = "FILE" , required_if ( "out_type" , "file" ))] file_name : String , } fn main () { let opt = Opt :: from_args (); println ! ( "{:?}" , opt ); }

So derive(StructOpt) tells Rust to generate a command line parser, and the various structopt attributes are simply used for additional parameters.

First, define a struct, whatever its name. This structure corresponds to a clap::App , its fields correspond to clap::Arg (unless they're subcommands), and you can adjust these apps and args by #[structopt(...)] attributes.

Note:

Keep in mind that StructOpt trait is more than just from_args method. It has a number of additional features, including access to underlying clap::App via StructOpt::clap() . See the trait's reference documentation.

You can control the way structopt translates your struct into an actual clap::App invocation via #[structopt(...)] attributes.

The attributes fall into two categories:

structopt 's own magical methods. They are used by structopt itself. They come mostly in attr = ["whatever"] form, but some attr(args...) also exist.

raw attributes. They represent explicit clap::Arg/App method calls. They are what used to be explicit #[structopt(raw(...))] attrs in pre-0.3 structopt

Every structopt attribute looks like comma-separated sequence of methods:

ⓘ This example is not tested

#[ structopt ( short , long = "--long-option" , required_if ( "out" , "file" ), parse ( from_os_str = path :: to :: parser ) )]

#[structopt(...)] attributes can be placed on top of struct , enum , struct field or enum variant. Attributes on top of struct or enum represent clap::App method calls, field or variant attributes correspond to clap::Arg method calls.

In other words, the Opt struct from the example above will be turned into this (details omitted):

App :: new ( "example" ) . version ( "0.2.0" ) . about ( "An example of StructOpt usage." ) . arg ( Arg :: with_name ( "debug" ) . help ( "Activate debug mode" ) . short ( "debug" ) . long ( "debug" )) . arg ( Arg :: with_name ( "speed" ) . help ( "Set speed" ) . short ( "v" ) . long ( "velocity" ) . default_value ( "42" ))

They are the reason why structopt is so flexible. Every and each method from clap::App/Arg can be used this way!

ⓘ This example is not tested

#[ structopt ( global = true , required_if ( "out" , "file" ) )]

The first form can only be used for methods which take only one argument. The second form must be used with multi-arg methods, but can also be used with single-arg methods. These forms are identical otherwise.

As long as method_name is not one of the magical methods - it will be translated into a mere method call.

Note:

"Raw methods" are direct replacement for pre-0.3 structopt's #[structopt(raw(...))] attributes, any time you would have used a raw() attribute in 0.2 you should use raw method in 0.3.

Unfortunately, old raw attributes collide with clap::Arg::raw method. To explicitly warn users of this change we allow #[structopt(raw())] only with true or false literals (this method is supposed to be called only with true anyway).

They are the reason why structopt is so easy to use and convenient in most cases. Many of them have defaults, some of them get used even if not mentioned.

Methods may be used on "top level" (on top of a struct , enum or enum variant) and/or on "field-level" (on top of a struct field or inside of an enum variant). Top level (non-magical) methods correspond to App::method calls, field-level methods are Arg::method calls.

ⓘ This example is not tested

#[ structopt ( top_level )] struct Foo { #[ structopt ( field_level )] field : u32 } #[ structopt ( top_level )] enum Bar { #[ structopt ( top_level )] Pineapple { #[ structopt ( field_level )] chocolate : String }, #[ structopt ( top_level )] Orange , }

name : [name = expr] On top level: App::new(expr) . The binary name displayed in help messages. Defaults to the crate name given by Cargo. On field-level: Arg::with_name(expr) . The name for the argument the field stands for, this name appears in help messages. Defaults to a name, deduced from a field, see also rename_all .

version : [version = "version"] Usable only on top level: App::version("version" or env!(CARGO_PKG_VERSION)) . The version displayed in help messages. Defaults to the crate version given by Cargo. If CARGO_PKG_VERSION is not set no .version() calls will be generated unless requested.

no_version : no_version Usable only on top level. Prevents default App::version call, i.e when no version = "version" mentioned.

author : author [= "author"] Usable only on top level: App::author("author" or env!(CARGO_PKG_AUTHOR)) . Author/maintainer of the binary, this name appears in help messages. Defaults to the crate author given by cargo, but only when author explicitly mentioned.

about : about [= "about"] Usable only on top level: App::about("about" or env!(CARGO_PKG_DESCRIPTION)) . Short description of the binary, appears in help messages. Defaults to the crate description given by cargo, but only when about explicitly mentioned.

short : short [= "short-opt-name"] Usable only on field-level.

long : long [= "long-opt-name"] Usable only on field-level.

default_value : default_value [= "default value"] Usable only on field-level.

rename_all : [ rename_all = "kebab"/"snake"/"screaming-snake"/"camel"/"pascal"/"verbatim"] Usable both on top level and field level.

parse : parse(type [= path::to::parser::fn]) Usable only on field-level.

skip : skip [= expr] Usable only on field-level.

flatten : flatten Usable on field-level or single-typed tuple variants.

subcommand : subcommand Usable only on field-level.

external_subcommand Usable only on enum variants.

env : env [= str_literal] Usable only on field-level.

rename_all_env : [ rename_all_env = "kebab"/"snake"/"screaming-snake"/"camel"/"pascal"/"verbatim"] Usable both on top level and field level.

verbatim_doc_comment : verbatim_doc_comment Usable both on top level and field level.

One of major things that makes structopt so awesome is it's type magic. Do you want optional positional argument? Use Option<T> ! Or perhaps optional argument that optionally takes value ( [--opt=[val]] )? Use Option<Option<T>> !

Here is the table of types and clap methods they correspond to:

Type Effect Added method call to clap::Arg bool true if the flag is present .takes_value(false).multiple(false) Option<T: FromStr> optional positional argument or option .takes_value(true).multiple(false) Option<Option<T: FromStr>> optional option with optional value .takes_value(true).multiple(false).min_values(0).max_values(1) Vec<T: FromStr> list of options or the other positional arguments .takes_value(true).multiple(true) Option<Vec<T: FromStr> optional list of options .takes_values(true).multiple(true).min_values(0) T: FromStr required option or positional argument .takes_value(true).multiple(false).required(!has_default)

The FromStr trait is used to convert the argument to the given type, and the Arg::validator method is set to a method using to_string() ( FromStr::Err must implement std::fmt::Display ). If you would like to use a custom string parser other than FromStr , see the same titled section below.

Important:

Pay attention that only literal occurrence of this types is special, for example Option<T> is special while ::std::option::Option<T> is not.

If you need to avoid special casing you can make a type alias and use it in place of the said type.

Note:

bool cannot be used as positional argument unless you provide an explicit parser. If you need a positional bool, for example to parse true or false , you must annotate the field with explicit #[structopt(parse(...))] .

Thus, the speed argument is generated as:

clap :: Arg :: with_name ( "speed" ) . takes_value ( true ) . multiple ( false ) . required ( false ) . validator ( parse_validator :: < f64 > ) . short ( "v" ) . long ( "velocity" ) . help ( "Set speed" ) . default_value ( "42" );

There are three types of arguments that can be supplied to each (sub-)command:

short (e.g. -h ),

), long (e.g. --help )

) and positional.

Like clap, structopt defaults to creating positional arguments.

If you want to generate a long argument you can specify either long = $NAME , or just long to get a long flag generated using the field name. The generated casing style can be modified using the rename_all attribute. See the rename_all example for more.

For short arguments, short will use the first letter of the field name by default, but just like the long option it's also possible to use a custom letter through short = $LETTER .

If an argument is renamed using name = $NAME any following call to short or long will use the new name.

Attention: If these arguments are used without an explicit name the resulting flag is going to be renamed using kebab-case if the rename_all attribute was not specified previously. The same is true for subcommands with implicit naming through the related data structure.

use structopt :: StructOpt ; #[ derive ( StructOpt )] #[ structopt ( rename_all = "kebab-case" )] struct Opt { #[ structopt ( long )] foo_option : String , #[ structopt ( short )] bar_option : String , #[ structopt ( short = "z" , long = "baz" )] baz_option : String , #[ structopt ( name = "custom" , long , short )] custom_option : String , my_positional : String , #[ structopt ( skip )] skipped : u32 , }

In clap, default values for options can be specified via Arg::default_value .

Of course, you can use as a raw method:

#[ derive ( StructOpt )] struct Opt { #[ structopt ( default_value = "" , long )] prefix : String }

This is quite mundane and error-prone to type the "..." default by yourself, especially when the Rust ecosystem uses the Default trait for that. It would be wonderful to have structopt to take the Default_default and fill it for you. And yes, structopt can do that.

Unfortunately, default_value takes &str but Default::default gives us some Self value. We need to map Self to &str somehow.

structopt solves this problem via ToString trait.

To be able to use auto-default the type must implement both Default and ToString :

#[ derive ( StructOpt )] struct Opt { #[ structopt ( default_value , long )] prefix : String }

In clap, help messages for the whole binary can be specified via App::about and App::long_about while help messages for individual arguments can be specified via Arg::help and Arg::long_help ".

long_* variants are used when user calls the program with --help and "short" variants are used with -h flag. In structopt , you can use them via raw methods, for example:

#[ derive ( StructOpt )] #[ structopt ( about = "I am a program and I work, just pass `-h`" )] struct Foo { #[ structopt ( short , help = "Pass `-h` and you'll see me!" )] bar : String }

For convenience, doc comments can be used instead of raw methods (this example works exactly like the one above):

#[ derive ( StructOpt )] struct Foo { bar : String }

Doc comments on top-level will be turned into App::about/long_about call (see below), doc comments on field-level are Arg::help/long_help calls.

Important:

Raw methods have priority over doc comments!

Top level doc comments always generate App::about/long_about calls! If you really want to use the App::help/long_help methods (you likely don't), use a raw method to override the App::about call generated from the doc comment.

A message passed to [ App::long_help ] or [ Arg::long_about ] will be displayed whenever your program is called with --help instead of -h . Of course, you can use them via raw methods as described above.

The more convenient way is to use a so-called "long" doc comment:

#[ derive ( StructOpt )] struct Robo { #[ structopt ( long )] kill_all_humans : bool }

A long doc comment consists of three parts:

Short summary

A blank line (whitespace only)

Detailed description, all the rest

In other words, "long" doc comment consists of two or more paragraphs, with the first being a summary and the rest being the detailed description.

A long comment will result in two method calls, help(<summary>) and long_help(<whole comment>) , so clap will display the summary with -h and the whole help message on --help (see below).

So, the example above will be turned into this (details omitted):

clap :: App :: new ( "<name>" ) . about ( "Hi there, I'm Robo!" ) . long_about ( "Hi there, I'm Robo!



\ I like beeping, stumbling, eating your electricity,\ and making records of you singing in a shower.\ Pay up or I'll upload it to youtube!" )

The -h flag is not the same as --help .

-h corresponds to Arg::help/App::about and requests short "summary" messages while --help corresponds to Arg::long_help/App::long_about and requests more detailed, descriptive messages.

It is entirely up to clap what happens if you used only one of Arg::help / Arg::long_help , see clap 's documentation for these methods.

As of clap v2.33, if only a short message ( Arg::help ) or only a long ( Arg::long_help ) message is provided, clap will use it for both -h and --help. The same logic applies to about/long_about .

structopt applies some preprocessing to doc comments to ease the most common uses:

Strip leading and trailing whitespace from every line, if present.

Strip leading and trailing blank lines, if present.

Interpret each group of non-empty lines as a word-wrapped paragraph. We replace newlines within paragraphs with spaces to allow the output to be re-wrapped to the terminal width.

Strip any excess blank lines so that there is exactly one per paragraph break.

If the first paragraph ends in exactly one period, remove the trailing period (i.e. strip trailing periods but not trailing ellipses).

Sometimes you don't want this preprocessing to apply, for example the comment contains some ASCII art or markdown tables, you would need to preserve LFs along with blank lines and the leading/trailing whitespace. You can ask structopt to preserve them via #[structopt(verbatim_doc_comment)] attribute.

This attribute must be applied to each field separately, there's no global switch.

Important:

Keep in mind that structopt will still remove one leading space from each line, even if this attribute is present, to allow for a space between /// and the content.

Also, structopt will still remove leading and trailing blank lines so these formats are equivalent:

ⓘ This example is not tested

It is possible to specify an environment variable fallback option for an arguments so that its value is taken from the specified environment variable if not given through the command-line:

#[ derive ( StructOpt )] struct Foo { #[ structopt ( short , long , env = "PARAMETER_VALUE" )] parameter_value : String }

By default, values from the environment are shown in the help output (i.e. when invoking --help ):

$ cargo run -- --help ... OPTIONS: -p, --parameter-value <parameter-value> [env: PARAMETER_VALUE=env_value]

In some cases this may be undesirable, for example when being used for passing credentials or secret tokens. In those cases you can use hide_env_values to avoid having structopt emit the actual secret values:

#[ derive ( StructOpt )] struct Foo { #[ structopt ( long = "secret" , env = "SECRET_VALUE" , hide_env_values = true )] secret_value : String }

Environment variables tend to be called after the corresponding struct 's field, as in example above. The field is secret_value and the env var is "SECRET_VALUE"; the name is the same, except casing is different.

It's pretty tedious and error-prone to type the same name twice, so you can ask structopt to do that for you.

#[ derive ( StructOpt )] struct Foo { #[ structopt ( long = "secret" , env )] secret_value : String }

It works just like #[structopt(short/long)] : if env is not set to some concrete value the value will be derived from the field's name. This is controlled by #[structopt(rename_all_env)] .

rename_all_env works exactly as rename_all (including overriding) except default casing is SCREAMING_SNAKE_CASE instead of kebab-case .

Sometimes you may want to add a field to your Opt struct that is not a command line option and clap should know nothing about it. You can ask structopt to skip the field entirely via #[structopt(skip = value)] ( value must implement Into<FieldType> ) or #[structopt(skip)] if you want assign the field with Default::default() (obviously, the field's type must implement Default ).

#[ derive ( StructOpt )] pub struct Opt { #[ structopt ( long , short )] number : u32 , #[ structopt ( skip )] k : String , #[ structopt ( skip )] v : Vec < u32 > , #[ structopt ( skip = vec ! [ 1 , 2 , 3 ] )] k2 : Vec < u32 > , #[ structopt ( skip = "cake" )] v2 : String , }

Some applications, especially large ones, split their functionality through the use of "subcommands". Each of these act somewhat like a separate command, but is part of the larger group. One example is git , which has subcommands such as add , commit , and clone , to mention just a few.

clap has this functionality, and structopt supports it through enums:

#[ derive ( StructOpt )] #[ structopt ( about = "the stupid content tracker" )] enum Git { Add { #[ structopt ( short )] interactive : bool , #[ structopt ( short )] patch : bool , #[ structopt ( parse ( from_os_str ))] files : Vec < PathBuf > }, Fetch { #[ structopt ( long )] dry_run : bool , #[ structopt ( long )] all : bool , repository : Option < String > }, Commit { #[ structopt ( short )] message : Option < String > , #[ structopt ( short )] all : bool } }

Using derive(StructOpt) on an enum instead of a struct will produce a clap::App that only takes subcommands. So git add , git fetch , and git commit would be commands allowed for the above example.

structopt also provides support for applications where certain flags need to apply to all subcommands, as well as nested subcommands:

#[ derive ( StructOpt )] struct MakeCookie { #[ structopt ( name = "supervisor" , default_value = "Puck" , long = "supervisor" )] supervising_faerie : String , tree : Option < String > , #[ structopt ( subcommand )] cmd : Command } #[ derive ( StructOpt )] enum Command { Pound { acorns : u32 }, Sparkle { #[ structopt ( short , parse ( from_occurrences ))] magicality : u64 , #[ structopt ( short )] color : String }, Finish ( Finish ), } #[ derive ( StructOpt )] struct Finish { #[ structopt ( short )] time : u32 , #[ structopt ( subcommand )] finish_type : FinishType } #[ derive ( StructOpt )] enum FinishType { Glaze { applications : u32 }, Powder { flavor : String , dips : u32 } }

Marking a field with structopt(subcommand) will add the subcommands of the designated enum to the current clap::App . The designated enum must also be derived StructOpt . So the above example would take the following commands:

make-cookie pound 50

make-cookie sparkle -mmm --color "green"

make-cookie finish 130 glaze 3

Subcommands may be optional:

#[ derive ( StructOpt )] struct Foo { file : String , #[ structopt ( subcommand )] cmd : Option < Command > } #[ derive ( StructOpt )] enum Command { Bar , Baz , Quux }

Sometimes you want to support not only the set of well-known subcommands but you also want to allow other, user-driven subcommands. clap supports this via AppSettings::AllowExternalSubcommands .

structopt provides it's own dedicated syntax for that:

#[ derive ( Debug , PartialEq , StructOpt )] struct Opt { #[ structopt ( subcommand )] sub : Subcommands , } #[ derive ( Debug , PartialEq , StructOpt )] enum Subcommands { Add , #[ structopt ( external_subcommand )] Other ( Vec < String > ), } assert_eq ! ( Opt :: from_iter ( & [ "test" , "add" ]), Opt { sub : Subcommands :: Add } ); assert_eq ! ( Opt :: from_iter ( & [ "test" , "git" , "status" ]), Opt { sub : Subcommands :: Other ( vec ! [ "git" . into (), "status" . into ()]) } ); assert ! ( Opt :: from_iter_safe ( & [ "test" ]). is_err ());

In other words, you just add an extra tuple variant marked with #[structopt(subcommand)] , and its type must be either Vec<String> or Vec<OsString> . structopt will detect String in this context and use appropriate clap API.

It is also possible to combine multiple enums of subcommands into one. All the subcommands will be on the same level.

#[ derive ( StructOpt )] enum BaseCli { Ghost10 { arg1 : i32 , } } #[ derive ( StructOpt )] enum Opt { #[ structopt ( flatten )] BaseCli ( BaseCli ), Dex { arg2 : i32 , } }

cli ghost10 42 cli dex 42

It can sometimes be useful to group related arguments in a substruct, while keeping the command-line interface flat. In these cases you can mark a field as flatten and give it another type that derives StructOpt :

#[ derive ( StructOpt )] struct Cmdline { #[ structopt ( short )] verbose : bool , #[ structopt ( flatten )] daemon_opts : DaemonOpts , } #[ derive ( StructOpt )] struct DaemonOpts { #[ structopt ( short )] user : String , #[ structopt ( short )] group : String , }

In this example, the derived Cmdline parser will support the options -v , -u and -g .

This feature also makes it possible to define a StructOpt struct in a library, parse the corresponding arguments in the main argument parser, and pass off this struct to a handler provided by that library.

If the field type does not have a FromStr implementation, or you would like to provide a custom parsing scheme other than FromStr , you may provide a custom string parser using parse(...) like this:

use std :: num :: ParseIntError ; use std :: path :: PathBuf ; fn parse_hex ( src : & str ) - > Result < u32 , ParseIntError > { u32 :: from_str_radix ( src , 16 ) } #[ derive ( StructOpt )] struct HexReader { #[ structopt ( short , parse ( try_from_str = parse_hex ))] number : u32 , #[ structopt ( short , parse ( from_os_str ))] output : PathBuf , }

There are five kinds of custom parsers:

Kind Signature Default from_str fn(&str) -> T ::std::convert::From::from try_from_str fn(&str) -> Result<T, E> ::std::str::FromStr::from_str from_os_str fn(&OsStr) -> T ::std::convert::From::from try_from_os_str fn(&OsStr) -> Result<T, OsString> (no default function) from_occurrences fn(u64) -> T value as T from_flag fn(bool) -> T ::std::convert::From::from

The from_occurrences parser is special. Using parse(from_occurrences) results in the number of flags occurrences being stored in the relevant field or being passed to the supplied function. In other words, it converts something like -vvv to 3 . This is equivalent to .takes_value(false).multiple(true) . Note that the default parser can only be used with fields of integer types ( u8 , usize , i64 , etc.).

The from_flag parser is also special. Using parse(from_flag) or parse(from_flag = some_func) will result in the field being treated as a flag even if it does not have type bool .

When supplying a custom string parser, bool will not be treated specially:

Type Effect Added method call to clap::Arg Option<T> optional argument .takes_value(true).multiple(false) Vec<T> list of arguments .takes_value(true).multiple(true) T required argument .takes_value(true).multiple(false).required(!has_default)