xaizek / dit (License: GPLv3) (since 2018-12-07)
Command-line task keeper that remembers all old values and is meant to combine several orthogonal features to be rather flexible in managing items.
Commit 67cce0cca6385335fcf6f0619a76fbac25d1bb1e

Extract breakIntoArgs() into utils/args
Author: xaizek
Author date (UTC): 2016-05-04 20:26
Committer name: xaizek
Committer date (UTC): 2016-05-05 12:04
Parent(s): cdb4f9275e909e4381c2613a2c75af05d04c4163
Signing key:
Tree: b5d1c7f777276083bf129d7af1ec5267f28ba2c6
File Lines added Lines deleted
src/Invocation.cpp 1 17
src/utils/args.hpp 19 13
tests/utils/args.cpp 12 13
File src/Invocation.cpp changed (mode: 100644) (index 51ef059..e8bd63b)
29 29 #include <boost/program_options/parsers.hpp> #include <boost/program_options/parsers.hpp>
30 30 #include <boost/program_options/variables_map.hpp> #include <boost/program_options/variables_map.hpp>
31 31 #include <boost/range/adaptor/reversed.hpp> #include <boost/range/adaptor/reversed.hpp>
32 #include <boost/tokenizer.hpp>
33 32
33 #include "utils/args.hpp"
34 34 #include "utils/containers.hpp" #include "utils/containers.hpp"
35 35 #include "utils/strings.hpp" #include "utils/strings.hpp"
36 36
37 37 namespace po = boost::program_options; namespace po = boost::program_options;
38 38
39 std::vector<std::string> breakIntoArgs(const std::string &line);
40 39 static std::vector<std::string> applyAlias( static std::vector<std::string> applyAlias(
41 40 const std::vector<std::string> &alias, const std::vector<std::string> &args, const std::vector<std::string> &alias, const std::vector<std::string> &args,
42 41 bool completion); bool completion);
 
... ... Invocation::processOptions(bool completion)
165 164 } }
166 165 } }
167 166
168 /**
169 * @brief Tokenize the command line, respecting escapes and quotes.
170 *
171 * @param line Line to parse.
172 *
173 * @returns Array of arguments.
174 */
175 std::vector<std::string>
176 breakIntoArgs(const std::string &line)
177 {
178 boost::escaped_list_separator<char> sep("\\", " ", "\"'");
179 boost::tokenizer<boost::escaped_list_separator<char>> tok(line, sep);
180 return std::vector<std::string>(tok.begin(), tok.end());
181 }
182
183 167 /** /**
184 168 * @brief Performs alias argument substitution. * @brief Performs alias argument substitution.
185 169 * *
File src/utils/args.hpp copied from file src/main.cpp (similarity 53%) (mode: 100644) (index b361b25..4524a64)
1 // Copyright (C) 2015 xaizek <xaizek@openmailbox.org>
1 // Copyright (C) 2016 xaizek <xaizek@openmailbox.org>
2 2 // //
3 3 // This file is part of dit. // This file is part of dit.
4 4 // //
 
15 15 // You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
16 16 // along with dit. If not, see <http://www.gnu.org/licenses/>. // along with dit. If not, see <http://www.gnu.org/licenses/>.
17 17
18 #include <cstdlib>
18 #ifndef DIT__UTILS__ARGS_HPP__
19 #define DIT__UTILS__ARGS_HPP__
19 20
20 #include <iostream>
21 #include <stdexcept>
21 #include <string>
22 22 #include <vector> #include <vector>
23 23
24 #include "Dit.hpp"
24 #include <boost/tokenizer.hpp>
25 25
26 int
27 main(int argc, char *argv[])
26 /**
27 * @brief Tokenize the command line, respecting escapes and quotes.
28 *
29 * @param line Line to parse.
30 *
31 * @returns Array of arguments.
32 */
33 inline std::vector<std::string>
34 breakIntoArgs(const std::string &line)
28 35 { {
29 try {
30 return Dit({ argv, argv + argc }).run();
31 } catch (std::exception &e) {
32 std::cerr << "Error: " << e.what() << std::endl;
33 return EXIT_FAILURE;
34 }
36 boost::escaped_list_separator<char> sep("\\", " ", "\"'");
37 boost::tokenizer<boost::escaped_list_separator<char>> tok(line, sep);
38 return { tok.begin(), tok.end() };
35 39 } }
40
41 #endif // DIT__UTILS__ARGS_HPP__
File tests/utils/args.cpp copied from file src/main.cpp (similarity 63%) (mode: 100644) (index b361b25..05d9a59)
1 // Copyright (C) 2015 xaizek <xaizek@openmailbox.org>
1 // Copyright (C) 2016 xaizek <xaizek@openmailbox.org>
2 2 // //
3 3 // This file is part of dit. // This file is part of dit.
4 4 // //
 
15 15 // You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
16 16 // along with dit. If not, see <http://www.gnu.org/licenses/>. // along with dit. If not, see <http://www.gnu.org/licenses/>.
17 17
18 #include <cstdlib>
18 #include "Catch/catch.hpp"
19 19
20 #include <iostream>
21 #include <stdexcept>
20 #include <string>
22 21 #include <vector> #include <vector>
23 22
24 #include "Dit.hpp"
23 #include "utils/args.hpp"
25 24
26 int
27 main(int argc, char *argv[])
25 TEST_CASE("Empty input", "[utils][args]")
28 26 { {
29 try {
30 return Dit({ argv, argv + argc }).run();
31 } catch (std::exception &e) {
32 std::cerr << "Error: " << e.what() << std::endl;
33 return EXIT_FAILURE;
34 }
27 REQUIRE(breakIntoArgs(std::string()).empty());
28 }
29
30 TEST_CASE("Benign input", "[utils][args]")
31 {
32 std::vector<std::string> args = breakIntoArgs("a b c");
33 REQUIRE(args == (std::vector<std::string>{ "a", "b", "c" }));
35 34 } }
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/dit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/dit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master