#arguments #shell #algorithm

nightly shtring

Split an input string into arguments by whitespace such that text between matching quotes is combined into a single argument

1 unstable release

0.1.0 Nov 6, 2020

#2151 in Parser implementations

MIT license

18KB
354 lines

shtring

Split an input string into arguments by whitespace such that text between matching quotes is combined into a single argument. Additionally, single character escapes are supported and ignored where applicable.

let input = "Hello world! \"This text will be a single argument.\" 'So \"will\" this.' \\\'Escaped quotes are ignored.\\\'";
let output = shtring::split(input)?;
assert_eq!(output, vec![
    "Hello",
    "world!",
    "This text will be a single argument.",
    "So \"will\" this.",
    "\\\'Escaped",
    "quotes",
    "are",
    "ignored.\\\'",
]);

lib.rs:

Split an input string into arguments by whitespace such that text between matching quotes is combined into a single argument. Additionally, single character escapes are supported and ignored where applicable.

let input =
    "Hello world! \"This text will be a single argument.\" 'So \"will\" this.' \\'Escaped quotes are ignored.\\'";
let output = split(input)?;
assert_eq!(
    output,
    vec![
        "Hello",
        "world!",
        "This text will be a single argument.",
        "So \"will\" this.",
        "\\'Escaped",
        "quotes",
        "are",
        "ignored.\\'",
    ]
);

The convenience function split is provided to easily turn an input string into a Vec over the parsed arguments, such that if the parser runs into an error, the parsing is aborted and that error is returned. For other cases, it is possible to create the Parser manually and iterate over the parsed arguments.

Dependencies

~315–770KB
~18K SLoC