pub fn parse(cmd: &str) -> Result<Vec<String>, Error> {
    let mut args: Vec<String> = Vec::new();
    let mut arg: String = String::new();
    let mut slash: bool = false;
    let mut quote = Quote::None;
    let mut data = false;
    for c in cmd.chars() {
        match c {
            '\'' if quote == Quote::Single => {
                quote = Quote::None;
            }
            _ if quote == Quote::Single => {
                arg.push(c);
            }
            ' ' | '\t' | '"' | '\'' | '\\' if slash => {
                arg.push(c);
                slash = false;
            }
            _ if slash => {
                arg.push('\\');
                arg.push(c);
                slash = false;
            }
            '"' if quote == Quote::Double => {
                quote = Quote::None;
            }
            ' ' | '\t' if quote == Quote::None => {
                if data {
                    args.push(arg);
                    arg = String::new();
                    data = false;
                }
            }
            '\\' => {
                slash = true;
                data = true;
            }
            '"' => {
                quote = Quote::Double;
                data = true;
            }
            '\'' => {
                quote = Quote::Single;
                data = true;
            }
            _ => {
                arg.push(c);
                data = true;
            }
        }
    }
    if data {
        args.push(arg);
    }
    if slash {
        return Err(Error::new(
            ErrorKind::InvalidInput,
            "Unexpected line end: escape sequence is not finished",
        ));
    }
    match quote {
        Quote::Single => Err(Error::new(
            ErrorKind::InvalidInput,
            "Unexpected line end: single quote is not closed",
        )),
        Quote::Double => Err(Error::new(
            ErrorKind::InvalidInput,
            "Unexpected line end: double quote is not closed",
        )),
        Quote::None => Ok(args),
    }
}
// Parsing command line arguments from singe line.
Parsing command line arguments from singe line.
Parsing command line arguments from singe line .
pub fn parse(cmd: &str) -> Result<Vec<String>, Error> {
    let mut args: Vec<String> = Vec::new();
    let mut arg: String = String::new();
    let mut slash: bool = false;
    let mut quote = Quote::None;
    let mut data = false;
    for c in cmd.chars() {
        match c {
            '\'' if quote == Quote::Single => {
                quote = Quote::None;
            }
            _ if quote == Quote::Single => {
                arg.push(c);
            }
            ' ' | '\t' | '"' | '\'' | '\\' if slash => {
                arg.push(c);
                slash = false;
            }
            _ if slash => {
                arg.push('\\');
                arg.push(c);
                slash = false;
            }
            '"' if quote == Quote::Double => {
                quote = Quote::None;
            }
            ' ' | '\t' if quote == Quote::None => {
                if data {
                    args.push(arg);
                    arg = String::new();
                    data = false;
                }
            }
            '\\' => {
                slash = true;
                data = true;
            }
            '"' => {
                quote = Quote::Double;
                data = true;
            }
            '\'' => {
                quote = Quote::Single;
                data = true;
            }
            _ => {
                arg.push(c);
                data = true;
            }
        }
    }
    if data {
        args.push(arg);
    }
    if slash {
        return Err(Error::new(
            ErrorKind::InvalidInput,
            "Unexpected line end: escape sequence is not finished",
        ));
    }
    match quote {
        Quote::Single => Err(Error::new(
            ErrorKind::InvalidInput,
            "Unexpected line end: single quote is not closed",
        )),
        Quote::Double => Err(Error::new(
            ErrorKind::InvalidInput,
            "Unexpected line end: double quote is not closed",
        )),
        Quote::None => Ok(args),
    }
}
pub fn parse ( cmd : & str ) -> Result < Vec < String > , Error > { let mut args : Vec < String > = Vec :: new ( ) ; let mut arg : String = String :: new ( ) ; let mut slash : bool = false ; let mut quote = Quote :: None ; let mut data = false ; for c in cmd . chars ( ) { match c { '\'' if quote == Quote :: Single => { quote = Quote :: None ; } _ if quote == Quote :: Single => { arg . push ( c ) ; } ' ' | '\t' | '"' | '\'' | '\\' if slash => { arg . push ( c ) ; slash = false ; } _ if slash => { arg . push ( '\\' ) ; arg . push ( c ) ; slash = false ; } '"' if quote == Quote :: Double => { quote = Quote :: None ; } ' ' | '\t' if quote == Quote :: None => { if data { args . push ( arg ) ; arg = String :: new ( ) ; data = false ; } } '\\' => { slash = true ; data = true ; } '"' => { quote = Quote :: Double ; data = true ; } '\'' => { quote = Quote :: Single ; data = true ; } _ => { arg . push ( c ) ; data = true ; } } } if data { args . push ( arg ) ; } if slash { return Err ( Error :: new ( ErrorKind :: InvalidInput , " " , ) ) ; } match quote { Quote :: Single => Err ( Error :: new ( ErrorKind :: InvalidInput , " " , ) ) , Quote :: Double => Err ( Error :: new ( ErrorKind :: InvalidInput , " " , ) ) , Quote :: None => Ok ( args ) , } }
Parsing command line arguments from singe line.
Parsing command line arguments from singe line .
cmd
&str
cmd
&str