wilhelmtell / shell_word_split

Emulate shell word splitting, for testing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

 shell_word_split

Parse words from a string, emulating word-splitting as the shell does.

 std::string in("echo hello world");
 std::vector<std::string> words;
 sws::shell_word_split(in.begin(), in.end(), std::back_inserter(words));
 for( auto const w : words )
     std::cout << '"' << w << "\" ";  // "echo" "hello" "world"
 std::cout << words.size() << '\n';   // 3

 in = "echo  	\  \ 5";  words.clear();
 sws::shell_word_split(in.begin(), in.end(), std::back_inserter(words));
 for( auto const w : words )
     std::cout << '"' << w << "\" ";  // "echo" " " " 5"
 std::cout << words.size() << '\n';   // 3

 in = "\\"hello there\\"";  words.clear();
 sws::shell_word_split(in.begin(), in.end(), std::back_inserter(words));
 for( auto const w : words )
     std::cout << '"' << w << "\" ";  // "hello there"
 std::cout << words.size() << '\n';   // 1

This is useful for me as a tool for testing commandline input in other
projects.

About

Emulate shell word splitting, for testing


Languages

Language:C++ 98.1%Language:Vim Script 1.9%