2
2
// This program is free software under MIT License.
3
3
// See the file LICENSE in this distribution for more details.
4
4
5
+ //! This crate is a library to parse command line arguments.
6
+ //!
7
+ //! This crate provides the following functionalities:
8
+ //!
9
+ //! - Supports [POSIX][posix] & [GNU][gnu] like short and long options.
10
+ //! - This crate supports `--` option.
11
+ //! - This library doesn't support numeric short option.
12
+ //! - This library supports not `-ofoo` but `-o=foo` as an alternative to
13
+ //! `-o foo` for short option.
14
+ //!
15
+ //! [posix]: https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html#Argument-Syntax
16
+ //! [gnu]: https://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html
17
+ //!
18
+ //! ## Install
19
+ //!
20
+ //! In `Cargo.toml`, write this crate as a dependency.
21
+ //!
22
+ //! ```toml
23
+ //! [dependencies]
24
+ //! cliargs = "0.1.0"
25
+ //! ```
26
+ //!
27
+ //! ## Usage
28
+ //!
29
+ //! This crate provides the `Cmd` strcut to parse command line arguments.
30
+ //! The usage of this `Cmd` struct is as follows:
31
+ //!
32
+ //! ### Creates a `Cmd` instance
33
+ //!
34
+ //! The `Cmd::new` function creates a `Cmd` instance with original command line
35
+ //! arguments.
36
+ //! This function uses `std::env::arg_os` and `OsString#into_string` to read
37
+ //! command line arguments in order to avoid `panic!` call that the user cannot
38
+ //! control.
39
+ //!
40
+ //! ```rust
41
+ //! use cliargs::Cmd;
42
+ //! use cliargs::errors::InvalidOsArg;
43
+ //!
44
+ //! let cmd = match Cmd::new() {
45
+ //! Ok(cmd) => cmd,
46
+ //! Err(InvalidOsArg::OsArgsContainInvalidUnicode { index, os_arg }) => {
47
+ //! panic!("Invalid Unicode data: {:?} (index: {})", os_arg, index);
48
+ //! }
49
+ //! };
50
+ //! ```
51
+ //!
52
+ //! ### Creates a `Cmd` instance with the specified `String` array
53
+ //!
54
+ //! The `Cmd::with_strings` function creates a `Cmd` instance with the
55
+ //! specified `String` array.
56
+ //!
57
+ //! ```rust
58
+ //! use cliargs::Cmd;
59
+ //! use std::env;
60
+ //!
61
+ //! let cmd = Cmd::with_strings(env::args());
62
+ //! ```
63
+ //!
64
+ //! ### Creates a `Cmd` instance with the specified `OsString` array.
65
+ //!
66
+ //! ```rust
67
+ //! use cliargs::Cmd;
68
+ //! use cliargs::errors::InvalidOsArg;
69
+ //! use std::env;
70
+ //!
71
+ //! let cmd = match Cmd::with_os_strings(env::args_os()) {
72
+ //! Ok(cmd) => cmd,
73
+ //! Err(InvalidOsArg::OsArgsContainInvalidUnicode { index, os_arg }) => {
74
+ //! panic!("Invalid Unicode data: {:?} (index: {})", os_arg, index);
75
+ //! }
76
+ //! };
77
+ //! ```
78
+ //!
79
+ //! ## Parses without configurations
80
+ //!
81
+ //! The `Cmd` struct has the method which parses command line arguments without
82
+ //! configurations.
83
+ //! This method automatically divides command line arguments to options and
84
+ //! command arguments.
85
+ //!
86
+ //! Command line arguments starts with `-` or `--` are options, and others are
87
+ //! command arguments.
88
+ //! If you want to specify a value to an option, follows `"="` and the value
89
+ //! after the option, like `foo=123`.
90
+ //!
91
+ //! All command line arguments after `--` are command arguments, even they
92
+ //! starts with `-` or `--`.
93
+ //!
94
+ //! ```rust
95
+ //! use cliargs::Cmd;
96
+ //! use cliargs::errors::InvalidOption;
97
+ //!
98
+ //! let mut cmd = Cmd::with_strings(vec![ /* ... */ ]);
99
+ //! match cmd.parse() {
100
+ //! Ok(_) => { /* ... */ },
101
+ //! Err(InvalidOption::OptionContainsInvalidChar { option }) => {
102
+ //! panic!("Option contains invalid character: {option}");
103
+ //! },
104
+ //! Err(err) => panic!("Invalid option: {}", err.option()),
105
+ //! }
106
+ //! ```
107
+
108
+ /// Enums for errors that can occur when parsing command line arguments.
5
109
pub mod errors;
6
110
7
111
mod parse;
@@ -13,6 +117,15 @@ use std::fmt;
13
117
use std:: mem;
14
118
use std:: path;
15
119
120
+ /// `Cmd` is the struct that parses command line arguments and stores them.
121
+ ///
122
+ /// The results of parsing are stored by separating into command name,
123
+ /// command arguments, options, and option arguments.
124
+ ///
125
+ /// These values are retrieved as string slices with the same lifetime as this
126
+ /// `Cmd` instance.
127
+ /// Therefore, if you want to use those values for a longer period, it is
128
+ /// needed to convert them to [String]s.
16
129
pub struct Cmd < ' a > {
17
130
name : & ' a str ,
18
131
args : Vec < & ' a str > ,
@@ -41,10 +154,20 @@ impl fmt::Debug for Cmd<'_> {
41
154
}
42
155
43
156
impl < ' a > Cmd < ' a > {
157
+ /// Creates a `Cmd` instance with command line arguments obtained from
158
+ /// [std::env::args_os].
159
+ ///
160
+ /// Since [std::env::args_os] returns a vector of [OsString] and they can
161
+ /// contain invalid unicode data, the return value of this funciton is
162
+ /// [Result] of `Cmd` or `errors::InvalidOsArg`.
44
163
pub fn new ( ) -> Result < Cmd < ' a > , errors:: InvalidOsArg > {
45
164
Self :: with_os_strings ( env:: args_os ( ) )
46
165
}
47
166
167
+ /// Creates a `Cmd` instance with the specified iterator of [OsString]s.
168
+ ///
169
+ /// [OsString]s can contain invalid unicode data, the return value of
170
+ /// this function is [Result] of `Cmd` or `errors::InvalidOsArg`.
48
171
pub fn with_os_strings (
49
172
osargs : impl IntoIterator < Item = OsString > ,
50
173
) -> Result < Cmd < ' a > , errors:: InvalidOsArg > {
@@ -113,6 +236,7 @@ impl<'a> Cmd<'a> {
113
236
} )
114
237
}
115
238
239
+ /// Creates a `Cmd` instance with the specified iterator of [String]s.
116
240
pub fn with_strings ( args : impl IntoIterator < Item = String > ) -> Cmd < ' a > {
117
241
let arg_iter = args. into_iter ( ) ;
118
242
let ( size, _) = arg_iter. size_hint ( ) ;
@@ -147,18 +271,34 @@ impl<'a> Cmd<'a> {
147
271
}
148
272
}
149
273
274
+ /// Returns the command name.
275
+ ///
276
+ /// This name is base name extracted from the command path string slice,
277
+ /// which is the first element of the command line arguments.
150
278
pub fn name ( & ' a self ) -> & ' a str {
151
279
self . name
152
280
}
153
281
282
+ /// Returns the command arguments.
283
+ ///
284
+ /// These arguments are retrieved as string slices in an array.
154
285
pub fn args ( & ' a self ) -> & ' a [ & ' a str ] {
155
286
& self . args
156
287
}
157
288
289
+ /// Checks whether an option with the specified name exists.
158
290
pub fn has_opt ( & self , name : & str ) -> bool {
159
291
self . opts . contains_key ( name)
160
292
}
161
293
294
+ /// Returns the option argument with the specified name.
295
+ ///
296
+ /// If the option has multiple arguments, this method returns the first
297
+ /// argument.
298
+ ///
299
+ /// Since the option may not be specified in the command line arguments,
300
+ /// the return value of this method is an [Option] of an option argument
301
+ /// or [None].
162
302
pub fn opt_arg ( & ' a self , name : & str ) -> Option < & ' a str > {
163
303
if let Some ( opt_vec) = self . opts . get ( name) {
164
304
if opt_vec. len ( ) > 0 {
@@ -168,6 +308,14 @@ impl<'a> Cmd<'a> {
168
308
None
169
309
}
170
310
311
+ /// Returns the option arguments with the specified name.
312
+ ///
313
+ /// If the option has one or multiple arguments, this method returns an
314
+ /// array of the arguments.
315
+ ///
316
+ /// Since the option may not be specified in the command line arguments,
317
+ /// the return value of this method is an [Option] of option arguments or
318
+ /// [None].
171
319
pub fn opt_args ( & ' a self , name : & str ) -> Option < & ' a [ & ' a str ] > {
172
320
match self . opts . get ( name) {
173
321
Some ( vec) => Some ( & vec) ,
0 commit comments