1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This class works with command lines: building and parsing.
6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches.
7 // Switches will precede all other arguments without switch prefixes.
8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value".
9 // An argument of "--" will terminate switch parsing during initialization,
10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix.
12 // There is a singleton read-only CommandLine that represents the command line
13 // that the current process was started with. It must be initialized in main().
15 #ifndef BASE_COMMAND_LINE_H_
16 #define BASE_COMMAND_LINE_H_
23 #include "base/base_export.h"
24 #include "build/build_config.h"
30 class BASE_EXPORT CommandLine
{
33 // The native command line string type.
34 typedef std::wstring StringType
;
35 #elif defined(OS_POSIX)
36 typedef std::string StringType
;
39 typedef StringType::value_type CharType
;
40 typedef std::vector
<StringType
> StringVector
;
41 typedef std::map
<std::string
, StringType
> SwitchMap
;
43 // A constructor for CommandLines that only carry switches and arguments.
44 enum NoProgram
{ NO_PROGRAM
};
45 explicit CommandLine(NoProgram no_program
);
47 // Construct a new command line with |program| as argv[0].
48 explicit CommandLine(const base::FilePath
& program
);
50 // Construct a new command line from an argument list.
51 CommandLine(int argc
, const CharType
* const* argv
);
52 explicit CommandLine(const StringVector
& argv
);
56 // Initialize the current process CommandLine singleton. On Windows, ignores
57 // its arguments (we instead parse GetCommandLineW() directly) because we
58 // don't trust the CRT's parsing of the command line, but it still must be
59 // called to set up the command line. Returns false if initialization has
60 // already occurred, and true otherwise. Only the caller receiving a 'true'
61 // return value should take responsibility for calling Reset.
62 static bool Init(int argc
, const char* const* argv
);
64 // Destroys the current process CommandLine singleton. This is necessary if
65 // you want to reset the base library to its initial state (for example, in an
66 // outer library that needs to be able to terminate, and be re-initialized).
67 // If Init is called only once, as in main(), Reset() is not necessary.
70 // Get the singleton CommandLine representing the current process's
71 // command line. Note: returned value is mutable, but not thread safe;
72 // only mutate if you know what you're doing!
73 static CommandLine
* ForCurrentProcess();
76 static CommandLine
FromString(const std::wstring
& command_line
);
79 // Initialize from an argv vector.
80 void InitFromArgv(int argc
, const CharType
* const* argv
);
81 void InitFromArgv(const StringVector
& argv
);
83 // Constructs and returns the represented command line string.
84 // CAUTION! This should be avoided on POSIX because quoting behavior is
86 StringType
GetCommandLineString() const;
88 // Constructs and returns the represented arguments string.
89 // CAUTION! This should be avoided on POSIX because quoting behavior is
91 StringType
GetArgumentsString() const;
93 // Returns the original command line string as a vector of strings.
94 const StringVector
& argv() const { return argv_
; }
96 // Get and Set the program part of the command line string (the first item).
97 base::FilePath
GetProgram() const;
98 void SetProgram(const base::FilePath
& program
);
100 // Returns true if this command line contains the given switch.
101 // (Switch names are case-insensitive).
102 bool HasSwitch(const std::string
& switch_string
) const;
104 // Returns the value associated with the given switch. If the switch has no
105 // value or isn't present, this method returns the empty string.
106 std::string
GetSwitchValueASCII(const std::string
& switch_string
) const;
107 base::FilePath
GetSwitchValuePath(const std::string
& switch_string
) const;
108 StringType
GetSwitchValueNative(const std::string
& switch_string
) const;
110 // Get a copy of all switches, along with their values.
111 const SwitchMap
& GetSwitches() const { return switches_
; }
113 // Append a switch [with optional value] to the command line.
114 // Note: Switches will precede arguments regardless of appending order.
115 void AppendSwitch(const std::string
& switch_string
);
116 void AppendSwitchPath(const std::string
& switch_string
,
117 const base::FilePath
& path
);
118 void AppendSwitchNative(const std::string
& switch_string
,
119 const StringType
& value
);
120 void AppendSwitchASCII(const std::string
& switch_string
,
121 const std::string
& value
);
123 // Copy a set of switches (and any values) from another command line.
124 // Commonly used when launching a subprocess.
125 void CopySwitchesFrom(const CommandLine
& source
,
126 const char* const switches
[],
129 // Get the remaining arguments to the command.
130 StringVector
GetArgs() const;
132 // Append an argument to the command line. Note that the argument is quoted
133 // properly such that it is interpreted as one argument to the target command.
134 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8.
135 // Note: Switches will precede arguments regardless of appending order.
136 void AppendArg(const std::string
& value
);
137 void AppendArgPath(const base::FilePath
& value
);
138 void AppendArgNative(const StringType
& value
);
140 // Append the switches and arguments from another command line to this one.
141 // If |include_program| is true, include |other|'s program as well.
142 void AppendArguments(const CommandLine
& other
, bool include_program
);
144 // Insert a command before the current command.
145 // Common for debuggers, like "valgrind" or "gdb --args".
146 void PrependWrapper(const StringType
& wrapper
);
149 // Initialize by parsing the given command line string.
150 // The program name is assumed to be the first item in the string.
151 void ParseFromString(const std::wstring
& command_line
);
155 // Disallow default constructor; a program name must be explicitly specified.
157 // Allow the copy constructor. A common pattern is to copy of the current
158 // process's command line and then add some flags to it. For example:
159 // CommandLine cl(*CommandLine::ForCurrentProcess());
160 // cl.AppendSwitch(...);
162 // The singleton CommandLine representing the current process's command line.
163 static CommandLine
* current_process_commandline_
;
165 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* }
168 // Parsed-out switch keys and values.
171 // The index after the program and switches, any arguments start here.
175 #endif // BASE_COMMAND_LINE_H_