michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: // This class works with command lines: building and parsing. michael@0: // Switches can optionally have a value attached using an equals sign, michael@0: // as in "-switch=value". Arguments that aren't prefixed with a michael@0: // switch prefix are considered "loose parameters". Switch names are michael@0: // case-insensitive. An argument of "--" will terminate switch michael@0: // parsing, causing everything after to be considered as loose michael@0: // parameters. michael@0: michael@0: // There is a singleton read-only CommandLine that represents the command michael@0: // line that the current process was started with. It must be initialized michael@0: // in main() (or whatever the platform's equivalent function is). michael@0: michael@0: #ifndef BASE_COMMAND_LINE_H_ michael@0: #define BASE_COMMAND_LINE_H_ michael@0: michael@0: #include "build/build_config.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "base/logging.h" michael@0: michael@0: class InProcessBrowserTest; michael@0: michael@0: class CommandLine { michael@0: public: michael@0: #if defined(OS_WIN) michael@0: // Creates a parsed version of the given command-line string. michael@0: // The program name is assumed to be the first item in the string. michael@0: void ParseFromString(const std::wstring& command_line); michael@0: #elif defined(OS_POSIX) michael@0: // Initialize from an argv vector (or directly from main()'s argv). michael@0: CommandLine(int argc, const char* const* argv); michael@0: explicit CommandLine(const std::vector& argv); michael@0: #endif michael@0: michael@0: // Construct a new, empty command line. michael@0: // |program| is the name of the program to run (aka argv[0]). michael@0: // TODO(port): should be a FilePath. michael@0: explicit CommandLine(const std::wstring& program); michael@0: michael@0: // Initialize the current process CommandLine singleton. On Windows, michael@0: // ignores its arguments (we instead parse GetCommandLineW() michael@0: // directly) because we don't trust the CRT's parsing of the command michael@0: // line, but it still must be called to set up the command line. michael@0: static void Init(int argc, const char* const* argv); michael@0: michael@0: // Destroys the current process CommandLine singleton. This is necessary if michael@0: // you want to reset the base library to its initial state (for example in an michael@0: // outer library that needs to be able to terminate, and be re-initialized). michael@0: // If Init is called only once, e.g. in main(), calling Terminate() is not michael@0: // necessary. michael@0: static void Terminate(); michael@0: michael@0: // Get the singleton CommandLine representing the current process's michael@0: // command line. michael@0: static const CommandLine* ForCurrentProcess() { michael@0: DCHECK(current_process_commandline_); michael@0: return current_process_commandline_; michael@0: } michael@0: michael@0: static bool IsInitialized() { michael@0: return !!current_process_commandline_; michael@0: } michael@0: michael@0: // Returns true if this command line contains the given switch. michael@0: // (Switch names are case-insensitive.) michael@0: bool HasSwitch(const std::wstring& switch_string) const; michael@0: michael@0: // Returns the value associated with the given switch. If the michael@0: // switch has no value or isn't present, this method returns michael@0: // the empty string. michael@0: std::wstring GetSwitchValue(const std::wstring& switch_string) const; michael@0: michael@0: // Get the remaining arguments to the command. michael@0: // WARNING: this is incorrect on POSIX; we must do string conversions. michael@0: std::vector GetLooseValues() const; michael@0: michael@0: #if defined(OS_WIN) michael@0: // Returns the original command line string. michael@0: const std::wstring& command_line_string() const { michael@0: return command_line_string_; michael@0: } michael@0: #elif defined(OS_POSIX) michael@0: // Returns the original command line string as a vector of strings. michael@0: const std::vector& argv() const { michael@0: return argv_; michael@0: } michael@0: #endif michael@0: michael@0: // Returns the program part of the command line string (the first item). michael@0: std::wstring program() const; michael@0: michael@0: // Return a copy of the string prefixed with a switch prefix. michael@0: // Used internally. michael@0: static std::wstring PrefixedSwitchString(const std::wstring& switch_string); michael@0: michael@0: // Return a copy of the string prefixed with a switch prefix, michael@0: // and appended with the given value. Used internally. michael@0: static std::wstring PrefixedSwitchStringWithValue( michael@0: const std::wstring& switch_string, michael@0: const std::wstring& value_string); michael@0: michael@0: // Appends the given switch string (preceded by a space and a switch michael@0: // prefix) to the given string. michael@0: void AppendSwitch(const std::wstring& switch_string); michael@0: michael@0: // Appends the given switch string (preceded by a space and a switch michael@0: // prefix) to the given string, with the given value attached. michael@0: void AppendSwitchWithValue(const std::wstring& switch_string, michael@0: const std::wstring& value_string); michael@0: michael@0: // Append a loose value to the command line. michael@0: void AppendLooseValue(const std::wstring& value); michael@0: michael@0: // Append the arguments from another command line to this one. michael@0: // If |include_program| is true, include |other|'s program as well. michael@0: void AppendArguments(const CommandLine& other, michael@0: bool include_program); michael@0: michael@0: // On POSIX systems it's common to run processes via a wrapper (like michael@0: // "valgrind" or "gdb --args"). michael@0: void PrependWrapper(const std::wstring& wrapper); michael@0: michael@0: private: michael@0: friend class InProcessBrowserTest; michael@0: michael@0: CommandLine() {} michael@0: michael@0: // Used by InProcessBrowserTest. michael@0: static CommandLine* ForCurrentProcessMutable() { michael@0: DCHECK(current_process_commandline_); michael@0: return current_process_commandline_; michael@0: } michael@0: michael@0: // The singleton CommandLine instance representing the current process's michael@0: // command line. michael@0: static CommandLine* current_process_commandline_; michael@0: michael@0: // We store a platform-native version of the command line, used when building michael@0: // up a new command line to be executed. This ifdef delimits that code. michael@0: michael@0: #if defined(OS_WIN) michael@0: // The quoted, space-separated command-line string. michael@0: std::wstring command_line_string_; michael@0: michael@0: // The name of the program. michael@0: std::wstring program_; michael@0: michael@0: // The type of native command line arguments. michael@0: typedef std::wstring StringType; michael@0: michael@0: #elif defined(OS_POSIX) michael@0: // The argv array, with the program name in argv_[0]. michael@0: std::vector argv_; michael@0: michael@0: // The type of native command line arguments. michael@0: typedef std::string StringType; michael@0: michael@0: // Shared by the two POSIX constructor forms. Initalize from argv_. michael@0: void InitFromArgv(); michael@0: #endif michael@0: michael@0: // Returns true and fills in |switch_string| and |switch_value| michael@0: // if |parameter_string| represents a switch. michael@0: static bool IsSwitch(const StringType& parameter_string, michael@0: std::string* switch_string, michael@0: StringType* switch_value); michael@0: michael@0: // Parsed-out values. michael@0: std::map switches_; michael@0: michael@0: // Non-switch command-line arguments. michael@0: std::vector loose_values_; michael@0: michael@0: // We allow copy constructors, because a common pattern is to grab a michael@0: // copy of the current process's command line and then add some michael@0: // flags to it. E.g.: michael@0: // CommandLine cl(*CommandLine::ForCurrentProcess()); michael@0: // cl.AppendSwitch(...); michael@0: }; michael@0: michael@0: #endif // BASE_COMMAND_LINE_H_