ipc/chromium/src/base/command_line.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 // This class works with command lines: building and parsing.
michael@0 6 // Switches can optionally have a value attached using an equals sign,
michael@0 7 // as in "-switch=value". Arguments that aren't prefixed with a
michael@0 8 // switch prefix are considered "loose parameters". Switch names are
michael@0 9 // case-insensitive. An argument of "--" will terminate switch
michael@0 10 // parsing, causing everything after to be considered as loose
michael@0 11 // parameters.
michael@0 12
michael@0 13 // There is a singleton read-only CommandLine that represents the command
michael@0 14 // line that the current process was started with. It must be initialized
michael@0 15 // in main() (or whatever the platform's equivalent function is).
michael@0 16
michael@0 17 #ifndef BASE_COMMAND_LINE_H_
michael@0 18 #define BASE_COMMAND_LINE_H_
michael@0 19
michael@0 20 #include "build/build_config.h"
michael@0 21
michael@0 22 #include <map>
michael@0 23 #include <string>
michael@0 24 #include <vector>
michael@0 25
michael@0 26 #include "base/basictypes.h"
michael@0 27 #include "base/logging.h"
michael@0 28
michael@0 29 class InProcessBrowserTest;
michael@0 30
michael@0 31 class CommandLine {
michael@0 32 public:
michael@0 33 #if defined(OS_WIN)
michael@0 34 // Creates a parsed version of the given command-line string.
michael@0 35 // The program name is assumed to be the first item in the string.
michael@0 36 void ParseFromString(const std::wstring& command_line);
michael@0 37 #elif defined(OS_POSIX)
michael@0 38 // Initialize from an argv vector (or directly from main()'s argv).
michael@0 39 CommandLine(int argc, const char* const* argv);
michael@0 40 explicit CommandLine(const std::vector<std::string>& argv);
michael@0 41 #endif
michael@0 42
michael@0 43 // Construct a new, empty command line.
michael@0 44 // |program| is the name of the program to run (aka argv[0]).
michael@0 45 // TODO(port): should be a FilePath.
michael@0 46 explicit CommandLine(const std::wstring& program);
michael@0 47
michael@0 48 // Initialize the current process CommandLine singleton. On Windows,
michael@0 49 // ignores its arguments (we instead parse GetCommandLineW()
michael@0 50 // directly) because we don't trust the CRT's parsing of the command
michael@0 51 // line, but it still must be called to set up the command line.
michael@0 52 static void Init(int argc, const char* const* argv);
michael@0 53
michael@0 54 // Destroys the current process CommandLine singleton. This is necessary if
michael@0 55 // you want to reset the base library to its initial state (for example in an
michael@0 56 // outer library that needs to be able to terminate, and be re-initialized).
michael@0 57 // If Init is called only once, e.g. in main(), calling Terminate() is not
michael@0 58 // necessary.
michael@0 59 static void Terminate();
michael@0 60
michael@0 61 // Get the singleton CommandLine representing the current process's
michael@0 62 // command line.
michael@0 63 static const CommandLine* ForCurrentProcess() {
michael@0 64 DCHECK(current_process_commandline_);
michael@0 65 return current_process_commandline_;
michael@0 66 }
michael@0 67
michael@0 68 static bool IsInitialized() {
michael@0 69 return !!current_process_commandline_;
michael@0 70 }
michael@0 71
michael@0 72 // Returns true if this command line contains the given switch.
michael@0 73 // (Switch names are case-insensitive.)
michael@0 74 bool HasSwitch(const std::wstring& switch_string) const;
michael@0 75
michael@0 76 // Returns the value associated with the given switch. If the
michael@0 77 // switch has no value or isn't present, this method returns
michael@0 78 // the empty string.
michael@0 79 std::wstring GetSwitchValue(const std::wstring& switch_string) const;
michael@0 80
michael@0 81 // Get the remaining arguments to the command.
michael@0 82 // WARNING: this is incorrect on POSIX; we must do string conversions.
michael@0 83 std::vector<std::wstring> GetLooseValues() const;
michael@0 84
michael@0 85 #if defined(OS_WIN)
michael@0 86 // Returns the original command line string.
michael@0 87 const std::wstring& command_line_string() const {
michael@0 88 return command_line_string_;
michael@0 89 }
michael@0 90 #elif defined(OS_POSIX)
michael@0 91 // Returns the original command line string as a vector of strings.
michael@0 92 const std::vector<std::string>& argv() const {
michael@0 93 return argv_;
michael@0 94 }
michael@0 95 #endif
michael@0 96
michael@0 97 // Returns the program part of the command line string (the first item).
michael@0 98 std::wstring program() const;
michael@0 99
michael@0 100 // Return a copy of the string prefixed with a switch prefix.
michael@0 101 // Used internally.
michael@0 102 static std::wstring PrefixedSwitchString(const std::wstring& switch_string);
michael@0 103
michael@0 104 // Return a copy of the string prefixed with a switch prefix,
michael@0 105 // and appended with the given value. Used internally.
michael@0 106 static std::wstring PrefixedSwitchStringWithValue(
michael@0 107 const std::wstring& switch_string,
michael@0 108 const std::wstring& value_string);
michael@0 109
michael@0 110 // Appends the given switch string (preceded by a space and a switch
michael@0 111 // prefix) to the given string.
michael@0 112 void AppendSwitch(const std::wstring& switch_string);
michael@0 113
michael@0 114 // Appends the given switch string (preceded by a space and a switch
michael@0 115 // prefix) to the given string, with the given value attached.
michael@0 116 void AppendSwitchWithValue(const std::wstring& switch_string,
michael@0 117 const std::wstring& value_string);
michael@0 118
michael@0 119 // Append a loose value to the command line.
michael@0 120 void AppendLooseValue(const std::wstring& value);
michael@0 121
michael@0 122 // Append the arguments from another command line to this one.
michael@0 123 // If |include_program| is true, include |other|'s program as well.
michael@0 124 void AppendArguments(const CommandLine& other,
michael@0 125 bool include_program);
michael@0 126
michael@0 127 // On POSIX systems it's common to run processes via a wrapper (like
michael@0 128 // "valgrind" or "gdb --args").
michael@0 129 void PrependWrapper(const std::wstring& wrapper);
michael@0 130
michael@0 131 private:
michael@0 132 friend class InProcessBrowserTest;
michael@0 133
michael@0 134 CommandLine() {}
michael@0 135
michael@0 136 // Used by InProcessBrowserTest.
michael@0 137 static CommandLine* ForCurrentProcessMutable() {
michael@0 138 DCHECK(current_process_commandline_);
michael@0 139 return current_process_commandline_;
michael@0 140 }
michael@0 141
michael@0 142 // The singleton CommandLine instance representing the current process's
michael@0 143 // command line.
michael@0 144 static CommandLine* current_process_commandline_;
michael@0 145
michael@0 146 // We store a platform-native version of the command line, used when building
michael@0 147 // up a new command line to be executed. This ifdef delimits that code.
michael@0 148
michael@0 149 #if defined(OS_WIN)
michael@0 150 // The quoted, space-separated command-line string.
michael@0 151 std::wstring command_line_string_;
michael@0 152
michael@0 153 // The name of the program.
michael@0 154 std::wstring program_;
michael@0 155
michael@0 156 // The type of native command line arguments.
michael@0 157 typedef std::wstring StringType;
michael@0 158
michael@0 159 #elif defined(OS_POSIX)
michael@0 160 // The argv array, with the program name in argv_[0].
michael@0 161 std::vector<std::string> argv_;
michael@0 162
michael@0 163 // The type of native command line arguments.
michael@0 164 typedef std::string StringType;
michael@0 165
michael@0 166 // Shared by the two POSIX constructor forms. Initalize from argv_.
michael@0 167 void InitFromArgv();
michael@0 168 #endif
michael@0 169
michael@0 170 // Returns true and fills in |switch_string| and |switch_value|
michael@0 171 // if |parameter_string| represents a switch.
michael@0 172 static bool IsSwitch(const StringType& parameter_string,
michael@0 173 std::string* switch_string,
michael@0 174 StringType* switch_value);
michael@0 175
michael@0 176 // Parsed-out values.
michael@0 177 std::map<std::string, StringType> switches_;
michael@0 178
michael@0 179 // Non-switch command-line arguments.
michael@0 180 std::vector<StringType> loose_values_;
michael@0 181
michael@0 182 // We allow copy constructors, because a common pattern is to grab a
michael@0 183 // copy of the current process's command line and then add some
michael@0 184 // flags to it. E.g.:
michael@0 185 // CommandLine cl(*CommandLine::ForCurrentProcess());
michael@0 186 // cl.AppendSwitch(...);
michael@0 187 };
michael@0 188
michael@0 189 #endif // BASE_COMMAND_LINE_H_

mercurial