| 1 | # shell-words |
| 2 | |
| 3 | Process command line according to parsing rules of Unix shell. |
| 4 | |
| 5 | ## Usage |
| 6 | |
| 7 | Add this to Cargo.toml: |
| 8 | ```toml |
| 9 | [dependencies] |
| 10 | shell-words = "1.0.0" |
| 11 | ``` |
| 12 | |
| 13 | Add this to your crate: |
| 14 | ```rust |
| 15 | extern crate shell_words; |
| 16 | ``` |
| 17 | |
| 18 | ## Examples |
| 19 | |
| 20 | ### Split |
| 21 | |
| 22 | Compiling C source code into an executable as in default build rule found in GNU Make: |
| 23 | |
| 24 | ```rust |
| 25 | extern crate shell_words; |
| 26 | |
| 27 | use std::env::var; |
| 28 | use std::process::Command; |
| 29 | |
| 30 | fn main() { |
| 31 | let cc = var("CC").unwrap_or_else(|_| "cc".to_owned()); |
| 32 | |
| 33 | let cflags = var("CFLAGS").unwrap_or_else(|_| String::new()); |
| 34 | let cflags = shell_words::split(&cflags).expect("failed to parse CFLAGS"); |
| 35 | |
| 36 | let cppflags = var("CPPFLAGS").unwrap_or_else(|_| String::new()); |
| 37 | let cppflags = shell_words::split(&cppflags).expect("failed to parse CPPFLAGS"); |
| 38 | |
| 39 | Command::new(cc) |
| 40 | .args(cflags) |
| 41 | .args(cppflags) |
| 42 | .args(&["-c", "a.c", "-o", "a.out"]) |
| 43 | .spawn() |
| 44 | .expect("failed to start subprocess") |
| 45 | .wait() |
| 46 | .expect("failed to wait for subprocess"); |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | ### Join |
| 51 | |
| 52 | Logging executed commands in format that can be readily copied and pasted to a shell: |
| 53 | |
| 54 | ```rust |
| 55 | extern crate shell_words; |
| 56 | |
| 57 | fn main() { |
| 58 | let argv = &["python", "-c", "print('Hello world!')"]; |
| 59 | |
| 60 | println!("Executing: {}", shell_words::join(argv)); |
| 61 | |
| 62 | std::process::Command::new(&argv[0]) |
| 63 | .args(&argv[1..]) |
| 64 | .spawn() |
| 65 | .expect("failed to start subprocess") |
| 66 | .wait() |
| 67 | .expect("failed to wait for subprocess"); |
| 68 | } |
| 69 | ``` |
| 70 | |
| 71 | ## Bugs |
| 72 | |
| 73 | Please report any issues at https://github.com/tmiasko/shell-words/issues. |
| 74 | |
| 75 | ## License |
| 76 | |
| 77 | Licensed under either of |
| 78 | |
| 79 | * Apache License, Version 2.0 |
| 80 | ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 81 | * MIT license |
| 82 | ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 83 | |
| 84 | at your option. |
| 85 | |
| 86 | ## Contribution |
| 87 | |
| 88 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 89 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 90 | dual licensed as above, without any additional terms or conditions. |