aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/include/winapi/cmd_line.hpp
blob: 8ea1a92ac9c5492366724d2da064f47b4daff279 (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
// 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 <boost/config.hpp>

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

namespace winapi {

class CommandLine {
public:
    static CommandLine query();

    static CommandLine parse(const std::string&);

    static CommandLine from_main(int argc, wchar_t* argv[]);

    CommandLine() = default;

    explicit CommandLine(const std::string& argv0, const std::vector<std::string>& args = {})
        : m_argv0(argv0), m_args(args) {}

    explicit CommandLine(std::string&& argv0, std::vector<std::string>&& args = {})
        : m_argv0(std::move(argv0)), m_args(std::move(args)) {}

    explicit CommandLine(const std::vector<std::string>& argv);

    explicit CommandLine(std::vector<std::string>&& argv);

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

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

    std::string to_string() const;

    std::string args_to_string() const;

    std::string get_argv0() const { return m_argv0; }

    bool has_args() const { return !get_args().empty(); }

    const std::vector<std::string>& get_args() const { return m_args; }

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

private:
    static BOOST_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