2 releases

0.1.1 Oct 30, 2018
0.1.0 Oct 22, 2018

#12 in #argv

34 downloads per month

Apache-2.0 OR LGPL-2.1-or-later

14KB
144 lines

r-shquote

POSIX Shell Compatible Argument Parser.

The r-shquote project implements quote and unquote operations for Shell compatible command-lines and prompts, as defined by the POSIX specification. Note that quirks and peculiarities of specific shell implementations are not supported.

Project

Requirements

The requirements for r-shquote are:

  • std (in particular alloc for string-allocations)

License

Apache Software License 2.0 Lesser General Public License 2.1+ See AUTHORS for details.


lib.rs:

POSIX Shell Compatible Argument Parser

This crate implements POSIX Shell compatible quote and unquote operations. These allow to quote arbitrary strings so they are not interpreted by a shell if taken as input. In the same way it allows unquoting these strings to get back the original input.

The way this quoting works is mostly standardized by POSIX. However, many existing implementations support additional features. These are explicitly not supported by this crate, and it is not the intention of this crate to support these quirks and peculiarities.

The basic operations provided are [quote()] and [unquote()], which both take a UTF-8 string as input, and produce the respective output string.

Examples

let str = "Hello World!";

println!("Quoted input: {}", r_shquote::quote(str));

Unquote operations can fail when the input is not well defined. The returned error contains diagnostics to identify where exactly the parser failed:

let quote = "'foobar";
let res = r_shquote::unquote(quote).unwrap_err();

println!("Unquote operation failed: {}", res);

Combining the quote and unquote operation always produces the original input:

let str = "foo bar";

assert_eq!(str, r_shquote::unquote(&r_shquote::quote(str)).unwrap());

No runtime deps