Skip to content

Commit 313d46a

Browse files
committed
Auto merge of #265 - kbknapp:issue-262, r=james-darkfox
Issue 262 - Result and Lossy options for invalid Unicode This allows not `panic!`ing on invalid unicode characters, and the option of returning lossy results. This does **not** turn all results into an `OsStr` preserving invalid unicode. If this helps with #262 we can merge (after review), otherwise we'll continue to discuss if there's a better way to handle this issue.
2 parents 3d0199d + 87ba544 commit 313d46a

File tree

9 files changed

+367
-141
lines changed

9 files changed

+367
-141
lines changed

src/app/app.rs

+241-63
Large diffs are not rendered by default.

src/app/errors.rs

+30-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub enum ClapErrorType {
88
/// Error occurs when some possible values were set, but clap found unexpected value
99
///
1010
///
11-
/// # Example
11+
/// # Examples
1212
///
1313
/// ```no_run
1414
/// # use clap::{App, Arg};
@@ -22,7 +22,7 @@ pub enum ClapErrorType {
2222
/// Error occurs when clap found unexpected flag or option
2323
///
2424
///
25-
/// # Example
25+
/// # Examples
2626
///
2727
/// ```no_run
2828
/// # use clap::{App, Arg};
@@ -34,7 +34,7 @@ pub enum ClapErrorType {
3434
/// Error occurs when clap found unexpected subcommand
3535
///
3636
///
37-
/// # Example
37+
/// # Examples
3838
///
3939
/// ```no_run
4040
/// # use clap::{App, Arg, SubCommand};
@@ -50,7 +50,7 @@ pub enum ClapErrorType {
5050
/// Error occurs when option does not allow empty values but some was found
5151
///
5252
///
53-
/// # Example
53+
/// # Examples
5454
///
5555
/// ```no_run
5656
/// # use clap::{App, Arg};
@@ -70,7 +70,7 @@ pub enum ClapErrorType {
7070
/// Error occurs when argument got more values then were expected
7171
///
7272
///
73-
/// # Example
73+
/// # Examples
7474
///
7575
/// ```no_run
7676
/// # use clap::{App, Arg};
@@ -83,7 +83,7 @@ pub enum ClapErrorType {
8383
/// Error occurs when argument got less values then were expected
8484
///
8585
///
86-
/// # Example
86+
/// # Examples
8787
///
8888
/// ```no_run
8989
/// # use clap::{App, Arg};
@@ -96,7 +96,7 @@ pub enum ClapErrorType {
9696
/// Error occurs when clap find two ore more conflicting arguments
9797
///
9898
///
99-
/// # Example
99+
/// # Examples
100100
///
101101
/// ```no_run
102102
/// # use clap::{App, Arg};
@@ -109,7 +109,7 @@ pub enum ClapErrorType {
109109
/// Error occurs when one or more required arguments missing
110110
///
111111
///
112-
/// # Example
112+
/// # Examples
113113
///
114114
/// ```no_run
115115
/// # use clap::{App, Arg};
@@ -122,7 +122,7 @@ pub enum ClapErrorType {
122122
/// Error occurs when required subcommand missing
123123
///
124124
///
125-
/// # Example
125+
/// # Examples
126126
///
127127
/// ```no_run
128128
/// # use clap::{App, Arg, AppSettings, SubCommand};
@@ -140,7 +140,7 @@ pub enum ClapErrorType {
140140
/// `AppSettings::ArgRequiredElseHelp` was used
141141
///
142142
///
143-
/// # Example
143+
/// # Examples
144144
///
145145
/// ```no_run
146146
/// # use clap::{App, Arg, AppSettings, SubCommand};
@@ -157,7 +157,7 @@ pub enum ClapErrorType {
157157
/// Error occurs when clap find argument while is was not expecting any
158158
///
159159
///
160-
/// # Example
160+
/// # Examples
161161
///
162162
/// ```no_run
163163
/// # use clap::{App};
@@ -168,7 +168,7 @@ pub enum ClapErrorType {
168168
/// Error occurs when argument was used multiple times and was not set as multiple.
169169
///
170170
///
171-
/// # Example
171+
/// # Examples
172172
///
173173
/// ```no_run
174174
/// # use clap::{App, Arg};
@@ -178,6 +178,24 @@ pub enum ClapErrorType {
178178
/// .get_matches_from_safe(vec!["", "--debug", "--debug"]);
179179
/// ```
180180
UnexpectedMultipleUsage,
181+
/// Error occurs when argument contains invalid unicode characters
182+
///
183+
///
184+
/// # Examples
185+
///
186+
/// ```no_run
187+
/// # use clap::{App, Arg};
188+
/// # use std::os::unix::ffi::OsStringExt;
189+
/// # use std::ffi::OsString;
190+
/// let result = App::new("myprog")
191+
/// .arg(Arg::with_name("debug")
192+
/// .short("u")
193+
/// .takes_value(true))
194+
/// .get_matches_from_safe(vec![OsString::from_vec(vec![0x20]),
195+
/// OsString::from_vec(vec![0xE9])]);
196+
/// assert!(result.is_err());
197+
/// ```
198+
InvalidUnicode
181199
}
182200

183201
/// Command line argument parser error

src/app/settings.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub enum AppSettings {
1010
///
1111
/// **NOTE:** This defaults to false (using subcommand does *not* negate requirements)
1212
///
13-
/// # Example
13+
/// # Examples
1414
///
1515
/// ```no_run
1616
/// # use clap::{App, AppSettings};
@@ -23,7 +23,7 @@ pub enum AppSettings {
2323
///
2424
/// **NOTE:** This defaults to false (subcommands do *not* need to be present)
2525
///
26-
/// # Example
26+
/// # Examples
2727
///
2828
/// ```no_run
2929
/// # use clap::{App, AppSettings};
@@ -37,7 +37,7 @@ pub enum AppSettings {
3737
///
3838
/// **NOTE:** Subcommands count as arguments
3939
///
40-
/// # Example
40+
/// # Examples
4141
///
4242
/// ```no_run
4343
/// # use clap::{App, AppSettings};
@@ -52,7 +52,7 @@ pub enum AppSettings {
5252
/// **NOTE:** The version for the current command and this setting must be set **prior** to
5353
/// adding any subcommands
5454
///
55-
/// # Example
55+
/// # Examples
5656
///
5757
/// ```no_run
5858
/// # use clap::{App, Arg, SubCommand, AppSettings};
@@ -72,7 +72,7 @@ pub enum AppSettings {
7272
///
7373
/// **NOTE:** Do not set this value to false, it will have undesired results!
7474
///
75-
/// # Example
75+
/// # Examples
7676
///
7777
/// ```no_run
7878
/// # use clap::{App, Arg, SubCommand, AppSettings};
@@ -90,7 +90,7 @@ pub enum AppSettings {
9090
///
9191
/// **NOTE:** This setting is cosmetic only and does not affect any functionality.
9292
///
93-
/// # Example
93+
/// # Examples
9494
///
9595
/// ```no_run
9696
/// # use clap::{App, Arg, SubCommand, AppSettings};
@@ -112,7 +112,7 @@ pub enum AppSettings {
112112
/// behavior for all subcommands, you must set this on each command (needing this is extremely
113113
/// rare)
114114
///
115-
/// # Example
115+
/// # Examples
116116
///
117117
/// ```no_run
118118
/// # use clap::{App, Arg, AppSettings};
@@ -131,7 +131,7 @@ pub enum AppSettings {
131131
/// still be displayed and exit. If this is *not* the desired result, consider using
132132
/// `.arg_required_else_help()`
133133
///
134-
/// # Example
134+
/// # Examples
135135
///
136136
/// ```no_run
137137
/// # use clap::{App, Arg, AppSettings};

0 commit comments

Comments
 (0)