1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/command_line.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,189 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +// This class works with command lines: building and parsing. 1.9 +// Switches can optionally have a value attached using an equals sign, 1.10 +// as in "-switch=value". Arguments that aren't prefixed with a 1.11 +// switch prefix are considered "loose parameters". Switch names are 1.12 +// case-insensitive. An argument of "--" will terminate switch 1.13 +// parsing, causing everything after to be considered as loose 1.14 +// parameters. 1.15 + 1.16 +// There is a singleton read-only CommandLine that represents the command 1.17 +// line that the current process was started with. It must be initialized 1.18 +// in main() (or whatever the platform's equivalent function is). 1.19 + 1.20 +#ifndef BASE_COMMAND_LINE_H_ 1.21 +#define BASE_COMMAND_LINE_H_ 1.22 + 1.23 +#include "build/build_config.h" 1.24 + 1.25 +#include <map> 1.26 +#include <string> 1.27 +#include <vector> 1.28 + 1.29 +#include "base/basictypes.h" 1.30 +#include "base/logging.h" 1.31 + 1.32 +class InProcessBrowserTest; 1.33 + 1.34 +class CommandLine { 1.35 + public: 1.36 +#if defined(OS_WIN) 1.37 + // Creates a parsed version of the given command-line string. 1.38 + // The program name is assumed to be the first item in the string. 1.39 + void ParseFromString(const std::wstring& command_line); 1.40 +#elif defined(OS_POSIX) 1.41 + // Initialize from an argv vector (or directly from main()'s argv). 1.42 + CommandLine(int argc, const char* const* argv); 1.43 + explicit CommandLine(const std::vector<std::string>& argv); 1.44 +#endif 1.45 + 1.46 + // Construct a new, empty command line. 1.47 + // |program| is the name of the program to run (aka argv[0]). 1.48 + // TODO(port): should be a FilePath. 1.49 + explicit CommandLine(const std::wstring& program); 1.50 + 1.51 + // Initialize the current process CommandLine singleton. On Windows, 1.52 + // ignores its arguments (we instead parse GetCommandLineW() 1.53 + // directly) because we don't trust the CRT's parsing of the command 1.54 + // line, but it still must be called to set up the command line. 1.55 + static void Init(int argc, const char* const* argv); 1.56 + 1.57 + // Destroys the current process CommandLine singleton. This is necessary if 1.58 + // you want to reset the base library to its initial state (for example in an 1.59 + // outer library that needs to be able to terminate, and be re-initialized). 1.60 + // If Init is called only once, e.g. in main(), calling Terminate() is not 1.61 + // necessary. 1.62 + static void Terminate(); 1.63 + 1.64 + // Get the singleton CommandLine representing the current process's 1.65 + // command line. 1.66 + static const CommandLine* ForCurrentProcess() { 1.67 + DCHECK(current_process_commandline_); 1.68 + return current_process_commandline_; 1.69 + } 1.70 + 1.71 + static bool IsInitialized() { 1.72 + return !!current_process_commandline_; 1.73 + } 1.74 + 1.75 + // Returns true if this command line contains the given switch. 1.76 + // (Switch names are case-insensitive.) 1.77 + bool HasSwitch(const std::wstring& switch_string) const; 1.78 + 1.79 + // Returns the value associated with the given switch. If the 1.80 + // switch has no value or isn't present, this method returns 1.81 + // the empty string. 1.82 + std::wstring GetSwitchValue(const std::wstring& switch_string) const; 1.83 + 1.84 + // Get the remaining arguments to the command. 1.85 + // WARNING: this is incorrect on POSIX; we must do string conversions. 1.86 + std::vector<std::wstring> GetLooseValues() const; 1.87 + 1.88 +#if defined(OS_WIN) 1.89 + // Returns the original command line string. 1.90 + const std::wstring& command_line_string() const { 1.91 + return command_line_string_; 1.92 + } 1.93 +#elif defined(OS_POSIX) 1.94 + // Returns the original command line string as a vector of strings. 1.95 + const std::vector<std::string>& argv() const { 1.96 + return argv_; 1.97 + } 1.98 +#endif 1.99 + 1.100 + // Returns the program part of the command line string (the first item). 1.101 + std::wstring program() const; 1.102 + 1.103 + // Return a copy of the string prefixed with a switch prefix. 1.104 + // Used internally. 1.105 + static std::wstring PrefixedSwitchString(const std::wstring& switch_string); 1.106 + 1.107 + // Return a copy of the string prefixed with a switch prefix, 1.108 + // and appended with the given value. Used internally. 1.109 + static std::wstring PrefixedSwitchStringWithValue( 1.110 + const std::wstring& switch_string, 1.111 + const std::wstring& value_string); 1.112 + 1.113 + // Appends the given switch string (preceded by a space and a switch 1.114 + // prefix) to the given string. 1.115 + void AppendSwitch(const std::wstring& switch_string); 1.116 + 1.117 + // Appends the given switch string (preceded by a space and a switch 1.118 + // prefix) to the given string, with the given value attached. 1.119 + void AppendSwitchWithValue(const std::wstring& switch_string, 1.120 + const std::wstring& value_string); 1.121 + 1.122 + // Append a loose value to the command line. 1.123 + void AppendLooseValue(const std::wstring& value); 1.124 + 1.125 + // Append the arguments from another command line to this one. 1.126 + // If |include_program| is true, include |other|'s program as well. 1.127 + void AppendArguments(const CommandLine& other, 1.128 + bool include_program); 1.129 + 1.130 + // On POSIX systems it's common to run processes via a wrapper (like 1.131 + // "valgrind" or "gdb --args"). 1.132 + void PrependWrapper(const std::wstring& wrapper); 1.133 + 1.134 + private: 1.135 + friend class InProcessBrowserTest; 1.136 + 1.137 + CommandLine() {} 1.138 + 1.139 + // Used by InProcessBrowserTest. 1.140 + static CommandLine* ForCurrentProcessMutable() { 1.141 + DCHECK(current_process_commandline_); 1.142 + return current_process_commandline_; 1.143 + } 1.144 + 1.145 + // The singleton CommandLine instance representing the current process's 1.146 + // command line. 1.147 + static CommandLine* current_process_commandline_; 1.148 + 1.149 + // We store a platform-native version of the command line, used when building 1.150 + // up a new command line to be executed. This ifdef delimits that code. 1.151 + 1.152 +#if defined(OS_WIN) 1.153 + // The quoted, space-separated command-line string. 1.154 + std::wstring command_line_string_; 1.155 + 1.156 + // The name of the program. 1.157 + std::wstring program_; 1.158 + 1.159 + // The type of native command line arguments. 1.160 + typedef std::wstring StringType; 1.161 + 1.162 +#elif defined(OS_POSIX) 1.163 + // The argv array, with the program name in argv_[0]. 1.164 + std::vector<std::string> argv_; 1.165 + 1.166 + // The type of native command line arguments. 1.167 + typedef std::string StringType; 1.168 + 1.169 + // Shared by the two POSIX constructor forms. Initalize from argv_. 1.170 + void InitFromArgv(); 1.171 +#endif 1.172 + 1.173 + // Returns true and fills in |switch_string| and |switch_value| 1.174 + // if |parameter_string| represents a switch. 1.175 + static bool IsSwitch(const StringType& parameter_string, 1.176 + std::string* switch_string, 1.177 + StringType* switch_value); 1.178 + 1.179 + // Parsed-out values. 1.180 + std::map<std::string, StringType> switches_; 1.181 + 1.182 + // Non-switch command-line arguments. 1.183 + std::vector<StringType> loose_values_; 1.184 + 1.185 + // We allow copy constructors, because a common pattern is to grab a 1.186 + // copy of the current process's command line and then add some 1.187 + // flags to it. E.g.: 1.188 + // CommandLine cl(*CommandLine::ForCurrentProcess()); 1.189 + // cl.AppendSwitch(...); 1.190 +}; 1.191 + 1.192 +#endif // BASE_COMMAND_LINE_H_