winapi_common
cmd_line.hpp
1 // Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
2 // This file is part of the "winapi-common" project.
3 // For details, see https://github.com/egor-tensin/winapi-common.
4 // Distributed under the MIT License.
5 
6 #pragma once
7 
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 namespace winapi {
13 
21 class CommandLine {
22 public:
24  static CommandLine query();
25 
30  static CommandLine parse(const std::string& src);
31 
37  static CommandLine from_main(int argc, wchar_t* argv[]);
38 
43  CommandLine() = default;
44 
50  explicit CommandLine(const std::string& argv0, const std::vector<std::string>& args = {})
51  : m_argv0{argv0}, m_args{args} {}
52 
58  explicit CommandLine(std::string&& argv0, std::vector<std::string>&& args = {})
59  : m_argv0{std::move(argv0)}, m_args{std::move(args)} {}
60 
65  explicit CommandLine(std::vector<std::string> argv);
66 
67  static std::string escape(const std::string&);
68 
69  static std::string escape_cmd(const std::string&);
70 
75  std::string to_string() const;
76 
81  std::string args_to_string() const;
82 
87  std::string get_argv0() const { return m_argv0; }
88 
90  bool has_args() const { return !get_args().empty(); }
91 
96  const std::vector<std::string>& get_args() const { return m_args; }
97 
102  std::vector<std::string> get_argv() const;
103 
104 private:
105  static constexpr char token_sep() { return ' '; }
106 
107  std::string escape_argv0() const { return escape(get_argv0()); }
108 
109  std::vector<std::string> escape_args() const;
110 
111  std::vector<std::string> escape_argv() const;
112 
113  std::string m_argv0;
114  std::vector<std::string> m_args;
115 };
116 
117 } // namespace winapi
Command line for the current process or for launching new processes.
Definition: cmd_line.hpp:21
static CommandLine from_main(int argc, wchar_t *argv[])
Definition: cmd_line.cpp:83
std::string get_argv0() const
Definition: cmd_line.hpp:87
CommandLine(const std::string &argv0, const std::vector< std::string > &args={})
Definition: cmd_line.hpp:50
static CommandLine query()
Definition: cmd_line.cpp:75
static CommandLine parse(const std::string &src)
Definition: cmd_line.cpp:79
CommandLine(std::string &&argv0, std::vector< std::string > &&args={})
Definition: cmd_line.hpp:58
std::vector< std::string > get_argv() const
Definition: cmd_line.cpp:146
std::string to_string() const
Definition: cmd_line.cpp:138
bool has_args() const
Definition: cmd_line.hpp:90
const std::vector< std::string > & get_args() const
Definition: cmd_line.hpp:96
std::string args_to_string() const
Definition: cmd_line.cpp:142