Skip to content

Commit

Permalink
Auto merge of #265 - kbknapp:issue-262, r=james-darkfox
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
homu committed Sep 22, 2015
2 parents 3d0199d + 87ba544 commit 313d46a
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 141 deletions.
304 changes: 241 additions & 63 deletions src/app/app.rs

Large diffs are not rendered by default.

42 changes: 30 additions & 12 deletions src/app/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub enum ClapErrorType {
/// Error occurs when some possible values were set, but clap found unexpected value
///
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg};
Expand All @@ -22,7 +22,7 @@ pub enum ClapErrorType {
/// Error occurs when clap found unexpected flag or option
///
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg};
Expand All @@ -34,7 +34,7 @@ pub enum ClapErrorType {
/// Error occurs when clap found unexpected subcommand
///
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg, SubCommand};
Expand All @@ -50,7 +50,7 @@ pub enum ClapErrorType {
/// Error occurs when option does not allow empty values but some was found
///
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg};
Expand All @@ -70,7 +70,7 @@ pub enum ClapErrorType {
/// Error occurs when argument got more values then were expected
///
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg};
Expand All @@ -83,7 +83,7 @@ pub enum ClapErrorType {
/// Error occurs when argument got less values then were expected
///
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg};
Expand All @@ -96,7 +96,7 @@ pub enum ClapErrorType {
/// Error occurs when clap find two ore more conflicting arguments
///
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg};
Expand All @@ -109,7 +109,7 @@ pub enum ClapErrorType {
/// Error occurs when one or more required arguments missing
///
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg};
Expand All @@ -122,7 +122,7 @@ pub enum ClapErrorType {
/// Error occurs when required subcommand missing
///
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg, AppSettings, SubCommand};
Expand All @@ -140,7 +140,7 @@ pub enum ClapErrorType {
/// `AppSettings::ArgRequiredElseHelp` was used
///
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg, AppSettings, SubCommand};
Expand All @@ -157,7 +157,7 @@ pub enum ClapErrorType {
/// Error occurs when clap find argument while is was not expecting any
///
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App};
Expand All @@ -168,7 +168,7 @@ pub enum ClapErrorType {
/// Error occurs when argument was used multiple times and was not set as multiple.
///
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg};
Expand All @@ -178,6 +178,24 @@ pub enum ClapErrorType {
/// .get_matches_from_safe(vec!["", "--debug", "--debug"]);
/// ```
UnexpectedMultipleUsage,
/// Error occurs when argument contains invalid unicode characters
///
///
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg};
/// # use std::os::unix::ffi::OsStringExt;
/// # use std::ffi::OsString;
/// let result = App::new("myprog")
/// .arg(Arg::with_name("debug")
/// .short("u")
/// .takes_value(true))
/// .get_matches_from_safe(vec![OsString::from_vec(vec![0x20]),
/// OsString::from_vec(vec![0xE9])]);
/// assert!(result.is_err());
/// ```
InvalidUnicode
}

/// Command line argument parser error
Expand Down
16 changes: 8 additions & 8 deletions src/app/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum AppSettings {
///
/// **NOTE:** This defaults to false (using subcommand does *not* negate requirements)
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, AppSettings};
Expand All @@ -23,7 +23,7 @@ pub enum AppSettings {
///
/// **NOTE:** This defaults to false (subcommands do *not* need to be present)
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, AppSettings};
Expand All @@ -37,7 +37,7 @@ pub enum AppSettings {
///
/// **NOTE:** Subcommands count as arguments
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, AppSettings};
Expand All @@ -52,7 +52,7 @@ pub enum AppSettings {
/// **NOTE:** The version for the current command and this setting must be set **prior** to
/// adding any subcommands
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg, SubCommand, AppSettings};
Expand All @@ -72,7 +72,7 @@ pub enum AppSettings {
///
/// **NOTE:** Do not set this value to false, it will have undesired results!
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg, SubCommand, AppSettings};
Expand All @@ -90,7 +90,7 @@ pub enum AppSettings {
///
/// **NOTE:** This setting is cosmetic only and does not affect any functionality.
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg, SubCommand, AppSettings};
Expand All @@ -112,7 +112,7 @@ pub enum AppSettings {
/// behavior for all subcommands, you must set this on each command (needing this is extremely
/// rare)
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg, AppSettings};
Expand All @@ -131,7 +131,7 @@ pub enum AppSettings {
/// still be displayed and exit. If this is *not* the desired result, consider using
/// `.arg_required_else_help()`
///
/// # Example
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg, AppSettings};
Expand Down
Loading

0 comments on commit 313d46a

Please sign in to comment.