Create a new-installs-only uniformity trial.
[chromium-blink-merge.git] / base / command_line.h
blob7d8627fa8417f33c77cc22c88e706ef821d78b50
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_
18 #include <stddef.h>
19 #include <map>
20 #include <string>
21 #include <vector>
23 #include "base/base_export.h"
24 #include "build/build_config.h"
26 class FilePath;
28 class BASE_EXPORT CommandLine {
29 public:
30 #if defined(OS_WIN)
31 // The native command line string type.
32 typedef std::wstring StringType;
33 #elif defined(OS_POSIX)
34 typedef std::string StringType;
35 #endif
37 typedef StringType::value_type CharType;
38 typedef std::vector<StringType> StringVector;
39 typedef std::map<std::string, StringType> SwitchMap;
41 // A constructor for CommandLines that only carry switches and arguments.
42 enum NoProgram { NO_PROGRAM };
43 explicit CommandLine(NoProgram no_program);
45 // Construct a new command line with |program| as argv[0].
46 explicit CommandLine(const FilePath& program);
48 // Construct a new command line from an argument list.
49 CommandLine(int argc, const CharType* const* argv);
50 explicit CommandLine(const StringVector& argv);
52 ~CommandLine();
54 // Initialize the current process CommandLine singleton. On Windows, ignores
55 // its arguments (we instead parse GetCommandLineW() directly) because we
56 // don't trust the CRT's parsing of the command line, but it still must be
57 // called to set up the command line. Returns false if initialization has
58 // already occurred, and true otherwise. Only the caller receiving a 'true'
59 // return value should take responsibility for calling Reset.
60 static bool Init(int argc, const char* const* argv);
62 // Destroys the current process CommandLine singleton. This is necessary if
63 // you want to reset the base library to its initial state (for example, in an
64 // outer library that needs to be able to terminate, and be re-initialized).
65 // If Init is called only once, as in main(), Reset() is not necessary.
66 static void Reset();
68 // Get the singleton CommandLine representing the current process's
69 // command line. Note: returned value is mutable, but not thread safe;
70 // only mutate if you know what you're doing!
71 static CommandLine* ForCurrentProcess();
73 #if defined(OS_WIN)
74 static CommandLine FromString(const std::wstring& command_line);
75 #endif
77 // Initialize from an argv vector.
78 void InitFromArgv(int argc, const CharType* const* argv);
79 void InitFromArgv(const StringVector& argv);
81 // Constructs and returns the represented command line string.
82 // CAUTION! This should be avoided on POSIX because quoting behavior is
83 // unclear.
84 StringType GetCommandLineString() const;
86 // Constructs and returns the represented arguments string.
87 // CAUTION! This should be avoided on POSIX because quoting behavior is
88 // unclear.
89 StringType GetArgumentsString() const;
91 // Returns the original command line string as a vector of strings.
92 const StringVector& argv() const { return argv_; }
94 // Get and Set the program part of the command line string (the first item).
95 FilePath GetProgram() const;
96 void SetProgram(const FilePath& program);
98 // Returns true if this command line contains the given switch.
99 // (Switch names are case-insensitive).
100 bool HasSwitch(const std::string& switch_string) const;
102 // Returns the value associated with the given switch. If the switch has no
103 // value or isn't present, this method returns the empty string.
104 std::string GetSwitchValueASCII(const std::string& switch_string) const;
105 FilePath GetSwitchValuePath(const std::string& switch_string) const;
106 StringType GetSwitchValueNative(const std::string& switch_string) const;
108 // Get a copy of all switches, along with their values.
109 const SwitchMap& GetSwitches() const { return switches_; }
111 // Append a switch [with optional value] to the command line.
112 // Note: Switches will precede arguments regardless of appending order.
113 void AppendSwitch(const std::string& switch_string);
114 void AppendSwitchPath(const std::string& switch_string, const FilePath& path);
115 void AppendSwitchNative(const std::string& switch_string,
116 const StringType& value);
117 void AppendSwitchASCII(const std::string& switch_string,
118 const std::string& value);
120 // Copy a set of switches (and any values) from another command line.
121 // Commonly used when launching a subprocess.
122 void CopySwitchesFrom(const CommandLine& source,
123 const char* const switches[],
124 size_t count);
126 // Get the remaining arguments to the command.
127 StringVector GetArgs() const;
129 // Append an argument to the command line. Note that the argument is quoted
130 // properly such that it is interpreted as one argument to the target command.
131 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8.
132 // Note: Switches will precede arguments regardless of appending order.
133 void AppendArg(const std::string& value);
134 void AppendArgPath(const FilePath& value);
135 void AppendArgNative(const StringType& value);
137 // Append the switches and arguments from another command line to this one.
138 // If |include_program| is true, include |other|'s program as well.
139 void AppendArguments(const CommandLine& other, bool include_program);
141 // Insert a command before the current command.
142 // Common for debuggers, like "valgrind" or "gdb --args".
143 void PrependWrapper(const StringType& wrapper);
145 #if defined(OS_WIN)
146 // Initialize by parsing the given command line string.
147 // The program name is assumed to be the first item in the string.
148 void ParseFromString(const std::wstring& command_line);
149 #endif
151 private:
152 // Disallow default constructor; a program name must be explicitly specified.
153 CommandLine();
154 // Allow the copy constructor. A common pattern is to copy of the current
155 // process's command line and then add some flags to it. For example:
156 // CommandLine cl(*CommandLine::ForCurrentProcess());
157 // cl.AppendSwitch(...);
159 // The singleton CommandLine representing the current process's command line.
160 static CommandLine* current_process_commandline_;
162 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* }
163 StringVector argv_;
165 // Parsed-out switch keys and values.
166 SwitchMap switches_;
168 // The index after the program and switches, any arguments start here.
169 size_t begin_args_;
172 #endif // BASE_COMMAND_LINE_H_