summaryrefslogtreecommitdiffstats
path: root/source/core/slang-command-line.h
blob: 804266ad1d6c650d1c831f11b608d676a3085e3d (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// slang-command-line.h
#ifndef SLANG_COMMAND_LINE_H
#define SLANG_COMMAND_LINE_H

#include "slang-list.h"
#include "slang-string.h"

namespace Slang
{

struct ExecutableLocation
{
    typedef ExecutableLocation ThisType;

    enum Type
    {
        Unknown, ///< Not specified
        Path,    ///< The executable is set as a path (ie won't be searched for)
        Name,    ///< The executable is passed as a name which will be searched for
    };

    /// Set the executable path.
    /// NOTE! On some targets the executable path *must* include an extension to be able to start as
    /// a process
    void setPath(const String& path)
    {
        m_type = Type::Path;
        m_pathOrName = path;
    }

    /// Set a filename (such that the path will be looked up)
    void setName(const String& filename)
    {
        m_type = Type::Name;
        m_pathOrName = filename;
    }

    void set(Type type, const String& pathOrName)
    {
        m_type = type;
        m_pathOrName = pathOrName;
    }

    /// Set the executable path from a base directory and an executable name (no suffix such as
    /// '.exe' needed)
    void set(const String& dir, const String& name);

    /// Determines if it's a name or a path when it sets
    void set(const String& nameOrPath);

    /// Append as text to out.
    void append(StringBuilder& out) const;

    /// Reset state to be same as ctor
    void reset()
    {
        m_type = Type::Unknown;
        m_pathOrName = String();
    }

    /// Equality means exactly the same definition.
    /// *NOT* that exactly the same executable is specified
    bool operator==(const ThisType& rhs) const
    {
        return m_type == rhs.m_type && m_pathOrName == rhs.m_pathOrName;
    }
    bool operator!=(const ThisType& rhs) const { return !(*this == rhs); }

    ExecutableLocation() {}
    ExecutableLocation(const String& dir, const String& name) { set(dir, name); }
    ExecutableLocation(Type type, const String& pathOrName)
        : m_type(type), m_pathOrName(pathOrName)
    {
    }

    explicit ExecutableLocation(const String& nameOrPath) { set(nameOrPath); }

    Type m_type = Type::Unknown;
    String m_pathOrName;
};

struct CommandLine
{
    typedef CommandLine ThisType;

    /// Add args - assumed unescaped
    void addArg(const String& in) { m_args.add(in); }
    void addArgs(const String* args, Int argsCount)
    {
        for (Int i = 0; i < argsCount; ++i)
            addArg(args[i]);
    }

    void addArgIfNotFound(const String& in);

    /// Find the index of an arg which is exact match for slice
    SLANG_INLINE Index findArgIndex(const UnownedStringSlice& slice) const
    {
        return m_args.indexOf(slice);
    }

    /// For handling args where the switch is placed directly in front of the path
    void addPrefixPathArg(
        const char* prefix,
        const String& path,
        const char* pathPostfix = nullptr);

    /// Get the total number of args
    SLANG_FORCE_INLINE Index getArgCount() const { return m_args.getCount(); }

    /// Reset to the initial state
    void reset() { *this = CommandLine(); }

    /// Append the args
    void appendArgs(StringBuilder& out) const;

    /// Append the command line to out
    void append(StringBuilder& out) const;
    /// convert into a string
    String toString() const;

    /// Convert just the args to string
    String toStringArgs() const;

    /// Set an executable location
    void setExecutableLocation(const ExecutableLocation& loc) { m_executableLocation = loc; }

    ExecutableLocation m_executableLocation; ///< The executable location
    List<String> m_args;                     ///< The arguments (Stored *unescaped*)
};

} // namespace Slang

#endif // SLANG_COMMAND_LINE_H