[M94 Dev][Tizen] Fix for errors for generating ninja files
[platform/framework/web/chromium-efl.git] / base / command_line.h
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.
4
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 // If a switch is specified multiple times, only the last value is used.
10 // An argument of "--" will terminate switch parsing during initialization,
11 // interpreting subsequent tokens as non-switch arguments, regardless of prefix.
12
13 // There is a singleton read-only CommandLine that represents the command line
14 // that the current process was started with.  It must be initialized in main().
15
16 #ifndef BASE_COMMAND_LINE_H_
17 #define BASE_COMMAND_LINE_H_
18
19 #include <stddef.h>
20 #include <map>
21 #include <string>
22 #include <vector>
23
24 #include "base/base_export.h"
25 #include "base/strings/string_piece.h"
26 #include "build/build_config.h"
27
28 namespace base {
29
30 class FilePath;
31
32 class BASE_EXPORT CommandLine {
33  public:
34 #if defined(OS_WIN)
35   // The native command line string type.
36   using StringType = std::wstring;
37 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
38   using StringType = std::string;
39 #endif
40
41   using CharType = StringType::value_type;
42   using StringPieceType = base::BasicStringPiece<CharType>;
43   using StringVector = std::vector<StringType>;
44   using SwitchMap = std::map<std::string, StringType, std::less<>>;
45
46   // A constructor for CommandLines that only carry switches and arguments.
47   enum NoProgram { NO_PROGRAM };
48   explicit CommandLine(NoProgram no_program);
49
50   // Construct a new command line with |program| as argv[0].
51   explicit CommandLine(const FilePath& program);
52
53   // Construct a new command line from an argument list.
54   CommandLine(int argc, const CharType* const* argv);
55   explicit CommandLine(const StringVector& argv);
56
57   CommandLine(const CommandLine& other);
58   CommandLine& operator=(const CommandLine& other);
59
60   ~CommandLine();
61
62 #if defined(OS_WIN)
63   // By default this class will treat command-line arguments beginning with
64   // slashes as switches on Windows, but not other platforms.
65   //
66   // If this behavior is inappropriate for your application, you can call this
67   // function BEFORE initializing the current process' global command line
68   // object and the behavior will be the same as Posix systems (only hyphens
69   // begin switches, everything else will be an arg).
70   static void set_slash_is_not_a_switch();
71
72   // Normally when the CommandLine singleton is initialized it gets the command
73   // line via the GetCommandLineW API and then uses the shell32 API
74   // CommandLineToArgvW to parse the command line and convert it back to
75   // argc and argv. Tests who don't want this dependency on shell32 and need
76   // to honor the arguments passed in should use this function.
77   static void InitUsingArgvForTesting(int argc, const char* const* argv);
78 #endif
79
80   // Initialize the current process CommandLine singleton. On Windows, ignores
81   // its arguments (we instead parse GetCommandLineW() directly) because we
82   // don't trust the CRT's parsing of the command line, but it still must be
83   // called to set up the command line. Returns false if initialization has
84   // already occurred, and true otherwise. Only the caller receiving a 'true'
85   // return value should take responsibility for calling Reset.
86   static bool Init(int argc, const char* const* argv);
87
88   // Destroys the current process CommandLine singleton. This is necessary if
89   // you want to reset the base library to its initial state (for example, in an
90   // outer library that needs to be able to terminate, and be re-initialized).
91   // If Init is called only once, as in main(), Reset() is not necessary.
92   // Do not call this in tests. Use base::test::ScopedCommandLine instead.
93   static void Reset();
94
95   // Get the singleton CommandLine representing the current process's
96   // command line. Note: returned value is mutable, but not thread safe;
97   // only mutate if you know what you're doing!
98   static CommandLine* ForCurrentProcess();
99
100   // Returns true if the CommandLine has been initialized for the given process.
101   static bool InitializedForCurrentProcess();
102
103 #if defined(OS_WIN)
104   static CommandLine FromString(StringPieceType command_line);
105 #endif
106
107   // Initialize from an argv vector.
108   void InitFromArgv(int argc, const CharType* const* argv);
109   void InitFromArgv(const StringVector& argv);
110
111   // Constructs and returns the represented command line string.
112   // CAUTION! This should be avoided on POSIX because quoting behavior is
113   // unclear.
114   // CAUTION! If writing a command line to the Windows registry, use
115   // GetCommandLineStringForShell() instead.
116   StringType GetCommandLineString() const;
117
118 #if defined(OS_WIN)
119   // Returns the command-line string in the proper format for the Windows shell,
120   // ending with the argument placeholder "--single-argument %1". The single-
121   // argument switch prevents unexpected parsing of arguments from other
122   // software that cannot be trusted to escape double quotes when substituting
123   // into a placeholder (e.g., "%1" insert sequences populated by the Windows
124   // shell).
125   // NOTE: this must be used to generate the command-line string for the shell
126   // even if this command line was parsed from a string with the proper syntax,
127   // because the --single-argument switch is not preserved during parsing.
128   StringType GetCommandLineStringForShell() const;
129
130   // Returns the represented command-line string. Allows the use of unsafe
131   // Windows insert sequences like "%1". Only use this method if
132   // GetCommandLineStringForShell() is not adequate AND the processor inserting
133   // the arguments is known to do so securely (i.e., is not the Windows shell).
134   // If in doubt, do not use.
135   StringType GetCommandLineStringWithUnsafeInsertSequences() const;
136 #endif
137
138   // Constructs and returns the represented arguments string.
139   // CAUTION! This should be avoided on POSIX because quoting behavior is
140   // unclear.
141   StringType GetArgumentsString() const;
142
143   // Returns the original command line string as a vector of strings.
144   const StringVector& argv() const { return argv_; }
145
146   // Get and Set the program part of the command line string (the first item).
147   FilePath GetProgram() const;
148   void SetProgram(const FilePath& program);
149
150   // Returns true if this command line contains the given switch.
151   // Switch names must be lowercase.
152   // The second override provides an optimized version to avoid inlining codegen
153   // at every callsite to find the length of the constant and construct a
154   // StringPiece.
155   bool HasSwitch(StringPiece switch_string) const;
156   bool HasSwitch(const char switch_constant[]) const;
157
158   // Returns the value associated with the given switch. If the switch has no
159   // value or isn't present, this method returns the empty string.
160   // Switch names must be lowercase.
161   std::string GetSwitchValueASCII(StringPiece switch_string) const;
162   FilePath GetSwitchValuePath(StringPiece switch_string) const;
163   StringType GetSwitchValueNative(StringPiece switch_string) const;
164
165   // Get a copy of all switches, along with their values.
166   const SwitchMap& GetSwitches() const { return switches_; }
167
168   // Append a switch [with optional value] to the command line.
169   // Note: Switches will precede arguments regardless of appending order.
170   void AppendSwitch(StringPiece switch_string);
171   void AppendSwitchPath(StringPiece switch_string, const FilePath& path);
172   void AppendSwitchNative(StringPiece switch_string, StringPieceType value);
173   void AppendSwitchASCII(StringPiece switch_string, StringPiece value);
174
175   // Removes the switch that matches |switch_key_without_prefix|, regardless of
176   // prefix and value. If no such switch is present, this has no effect.
177   void RemoveSwitch(const base::StringPiece switch_key_without_prefix);
178
179   // Copy a set of switches (and any values) from another command line.
180   // Commonly used when launching a subprocess.
181   void CopySwitchesFrom(const CommandLine& source,
182                         const char* const switches[],
183                         size_t count);
184
185   // Get the remaining arguments to the command.
186   StringVector GetArgs() const;
187
188   // Append an argument to the command line. Note that the argument is quoted
189   // properly such that it is interpreted as one argument to the target command.
190   // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8.
191   // Note: Switches will precede arguments regardless of appending order.
192   void AppendArg(StringPiece value);
193   void AppendArgPath(const FilePath& value);
194   void AppendArgNative(StringPieceType value);
195
196   // Append the switches and arguments from another command line to this one.
197   // If |include_program| is true, include |other|'s program as well.
198   void AppendArguments(const CommandLine& other, bool include_program);
199
200   // Insert a command before the current command.
201   // Common for debuggers, like "gdb --args".
202   void PrependWrapper(StringPieceType wrapper);
203
204 #if defined(OS_WIN)
205   // Initialize by parsing the given command line string.
206   // The program name is assumed to be the first item in the string.
207   void ParseFromString(StringPieceType command_line);
208 #endif
209
210  private:
211   // Disallow default constructor; a program name must be explicitly specified.
212   CommandLine() = delete;
213   // Allow the copy constructor. A common pattern is to copy of the current
214   // process's command line and then add some flags to it. For example:
215   //   CommandLine cl(*CommandLine::ForCurrentProcess());
216   //   cl.AppendSwitch(...);
217
218   // Append switches and arguments, keeping switches before arguments.
219   void AppendSwitchesAndArguments(const StringVector& argv);
220
221   // Internal version of GetArgumentsString to support allowing unsafe insert
222   // sequences in rare cases (see
223   // GetCommandLineStringWithUnsafeInsertSequences).
224   StringType GetArgumentsStringInternal(
225       bool allow_unsafe_insert_sequences) const;
226
227 #if defined(OS_WIN)
228   // Initializes by parsing |raw_command_line_string_|, treating everything
229   // after |single_arg_switch_string| + <a single character> as the command
230   // line's single argument, and dropping any arguments previously parsed. The
231   // command line must contain |single_arg_switch_string|, and the argument, if
232   // present, must be separated from |single_arg_switch_string| by one
233   // character.
234   // NOTE: the single-argument switch is not preserved after parsing;
235   // GetCommandLineStringForShell() must be used to reproduce the original
236   // command-line string with single-argument switch.
237   void ParseAsSingleArgument(const StringType& single_arg_switch_string);
238
239   // The string returned by GetCommandLineW(), to be parsed via
240   // ParseFromString(). Empty if this command line was not parsed from a string,
241   // or if ParseFromString() has finished executing.
242   StringPieceType raw_command_line_string_;
243 #endif
244
245   // The singleton CommandLine representing the current process's command line.
246   static CommandLine* current_process_commandline_;
247
248   // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* }
249   StringVector argv_;
250
251   // Parsed-out switch keys and values.
252   SwitchMap switches_;
253
254   // The index after the program and switches, any arguments start here.
255   size_t begin_args_;
256 };
257
258 }  // namespace base
259
260 #endif  // BASE_COMMAND_LINE_H_