aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/include/winapi/cmd_line.hpp
blob: d1a4a7e52945c2e9f07623707d56020e0ad4b6c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "winapi-common" project.
// For details, see https://github.com/egor-tensin/winapi-common.
// Distributed under the MIT License.

#pragma once

#include <string>
#include <utility>
#include <vector>

namespace winapi {

/**
 * @brief Command line for the current process or for launching new processes.
 *
 * This class takes care of proper parsing and stringifying command line
 * arguments so that they are safe to use with CreateProcess, ShellExecute,
 * etc.
 */
class CommandLine {
public:
    /** Get the command line used to launch this process. */
    static CommandLine query();

    /**
     * Parse a command line from a string.
     * @param src UTF-8 encoded string.
     */
    static CommandLine parse(const std::string& src);

    /**
     * Build a command line from main() arguments.
     * @param argc Length of the argv array.
     * @param argv UTF-16 encoded strings.
     */
    static CommandLine from_main(int argc, wchar_t* argv[]);

    /**
     * Build an empty command line.
     * It won't have neither argv[0], nor any other args.
     */
    CommandLine() = default;

    /**
     * Build a command line.
     * @param argv0 UTF-8 string, argv[0].
     * @param args  List of UTF-8 strings, other arguments.
     */
    explicit CommandLine(const std::string& argv0, const std::vector<std::string>& args = {})
        : m_argv0{argv0}, m_args{args} {}

    /**
     * Build a command line.
     * @param argv0 UTF-8 string, argv[0].
     * @param args  List of UTF-8 strings, other arguments.
     */
    explicit CommandLine(std::string&& argv0, std::vector<std::string>&& args = {})
        : m_argv0{std::move(argv0)}, m_args{std::move(args)} {}

    /**
     * Build a command line.
     * @param argv List of UTF-8 strings, including argv[0].
     */
    explicit CommandLine(std::vector<std::string> argv);

    static std::string escape(const std::string&);

    static std::string escape_cmd(const std::string&);

    /**
     * Build a string that represents this command line.
     * @return UTF-8 string.
     */
    std::string to_string() const;

    /**
     * Build a string that represents this command line, but omit argv[0].
     * @return UTF-8 string.
     */
    std::string args_to_string() const;

    /**
     * Get argv[0] for this command line.
     * @return UTF-8 string.
     */
    std::string get_argv0() const { return m_argv0; }

    /** Test if this command line has any additional arguments besides argv[0]. */
    bool has_args() const { return !get_args().empty(); }

    /**
     * Get list of arguments for this command line beyond argv[0].
     * @return List of UTF-8 strings.
     */
    const std::vector<std::string>& get_args() const { return m_args; }

    /**
     * Get list of arguments for this command line.
     * @return List of UTF-8 strings.
     */
    std::vector<std::string> get_argv() const;

private:
    static constexpr char token_sep() { return ' '; }

    std::string escape_argv0() const { return escape(get_argv0()); }

    std::vector<std::string> escape_args() const;

    std::vector<std::string> escape_argv() const;

    std::string m_argv0;
    std::vector<std::string> m_args;
};

} // namespace winapi