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