cmdline_parser/lib.rs
1//! Parse cmdlines in a way that resembles the platform default.
2//!
3//! # Example
4//!
5//! ```rust
6//! use cmdline_parser::Parser;
7//!
8//! let mut parser = Parser::new(r#"mv "my file" project/"#);
9//!
10//! assert_eq!(parser.next(), Some((0..2, "mv".into())));
11//! assert_eq!(parser.next(), Some((3..12, "my file".into())));
12//! assert_eq!(parser.next(), Some((13..21, "project/".into())));
13//! assert_eq!(parser.next(), None);
14//! ```
15
16pub mod unix;
17pub mod windows;
18
19#[cfg(unix)]
20pub use unix::Parser;
21#[cfg(windows)]
22pub use windows::Parser;
23
24/// Parse the given string as a single argument.
25///
26/// Resolves quoting and escaping, but does not split arguments.
27pub fn parse_single(argument: &str) -> String {
28 let mut parser = Parser::new(argument);
29 parser.set_separators(std::iter::empty());
30
31 parser.nth(0).map(|(_, arg)| arg).unwrap_or("".into())
32}