Skip to main content

split_arg_string

Function split_arg_string 

Source
pub fn split_arg_string(s: &str) -> Vec<String>
Expand description

Parse a string like a shell would: handle quotes, escapes, and whitespace.

This is useful for parsing command-line strings from environment variables or configuration files.

§Rules

  • Whitespace separates arguments
  • Single quotes (') preserve everything literally (no escape sequences)
  • Double quotes (") allow escape sequences (\", \\)
  • Backslash outside quotes escapes the next character
  • Empty quotes produce empty strings

§Example

use click::utils::split_arg_string;

let args = split_arg_string("foo 'bar baz' \"quoted\"");
assert_eq!(args, vec!["foo", "bar baz", "quoted"]);

let args = split_arg_string(r#"file\ name.txt "hello \"world\"""#);
assert_eq!(args, vec!["file name.txt", r#"hello "world""#]);

let args = split_arg_string("'single' \"double\" plain");
assert_eq!(args, vec!["single", "double", "plain"]);

§Reference

Based on Python Click’s shell_completion.py:split_arg_string.