1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
/*! This crate provides attribute macros for command-line argument parsing. # Usage Just by adding an attribute `#[cmd]` to a function, the function is converted to a command line program. ``` use argopt::cmd; #[cmd] fn main(host: String, port: u16) { // ... } ``` The output is: ```text $ cargo run error: The following required arguments were not provided: <host> <port> USAGE: argopt-test <host> <port> ``` You can customize the behavior of arguments by annotating them with attributes. ``` use argopt::cmd; #[cmd] fn main( #[opt(short = "h", long = "host")] host: String, #[opt(short, long, default_value = "80")] port: u16, ) { // ... } ``` And you can add help messages by adding doccomments. ``` use argopt::cmd; /// Sample program #[cmd] fn main( /// Host name #[opt(short = "h", long = "host")] host: String, /// Port number #[opt(short, long, default_value = "80")] port: u16, ) { // ... } ``` The output is: ```text argopt-test 0.1.0 Sample program USAGE: simple [OPTIONS] --host <host> FLAGS: --help Prints help information -V, --version Prints version information OPTIONS: -h, --host <host> Host name -p, --port <port> Port number [default: 80] ``` You can use the same options as [structopt](https://crates.io/crates/structopt). # Subcommands You can create sub commands by adding the attribute `#[subcmd]` to functions. ``` use argopt::{subcmd, cmd_group}; use std::path::PathBuf; #[subcmd] fn add( #[opt(short)] interactive: bool, #[opt(short)] patch: bool, files: Vec<PathBuf>, ) { // ... } #[subcmd] fn commit( #[opt(short)] message: Option<String>, #[opt(short)] all: bool, ) { // ... } #[cmd_group(commands = [add, commit])] fn main() {} ``` # Easy Verbosity Level Handling There is a feature that allows you to interact with the [log](https://crates.io/crates/log) crate and handle the verbosity level automatically. ``` use argopt::cmd; use log::*; #[cmd(verbose)] fn main() { error!("This is error"); warn!("This is warn"); info!("This is info"); debug!("This is debug"); trace!("This is trace"); } ``` The output is: ```text $ cargo run This is error $ cargo run -- -v This is error This is warn $ cargo run -- -vv This is error This is warn This is info $ cargo run -- -vvv This is error This is warn This is info This is debug $ cargo run -- -vvvv This is error This is warn This is info This is debug This is trace ``` */ pub use argopt_impl::{cmd, cmd_group, subcmd}; #[doc(hidden)] pub use structopt::StructOpt;