michael@0: // Copyright (c) 2012 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: // Arguments with prefixes ('--', '-', and on Windows, '/') are switches. michael@0: // Switches will precede all other arguments without switch prefixes. michael@0: // Switches can optionally have values, delimited by '=', e.g., "-switch=value". michael@0: // An argument of "--" will terminate switch parsing during initialization, michael@0: // interpreting subsequent tokens as non-switch arguments, regardless of prefix. michael@0: michael@0: // There is a singleton read-only CommandLine that represents the command line michael@0: // that the current process was started with. It must be initialized in main(). michael@0: michael@0: #ifndef BASE_COMMAND_LINE_H_ michael@0: #define BASE_COMMAND_LINE_H_ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "base/base_export.h" michael@0: #include "build/build_config.h" michael@0: michael@0: namespace base { michael@0: class FilePath; michael@0: } michael@0: michael@0: class BASE_EXPORT CommandLine { michael@0: public: michael@0: #if defined(OS_WIN) michael@0: // The native command line string type. michael@0: typedef std::wstring StringType; michael@0: #elif defined(OS_POSIX) michael@0: typedef std::string StringType; michael@0: #endif michael@0: michael@0: typedef StringType::value_type CharType; michael@0: typedef std::vector StringVector; michael@0: typedef std::map SwitchMap; michael@0: michael@0: // A constructor for CommandLines that only carry switches and arguments. michael@0: enum NoProgram { NO_PROGRAM }; michael@0: explicit CommandLine(NoProgram no_program); michael@0: michael@0: // Construct a new command line with |program| as argv[0]. michael@0: explicit CommandLine(const base::FilePath& program); michael@0: michael@0: // Construct a new command line from an argument list. michael@0: CommandLine(int argc, const CharType* const* argv); michael@0: explicit CommandLine(const StringVector& argv); michael@0: michael@0: ~CommandLine(); michael@0: michael@0: // Initialize the current process CommandLine singleton. On Windows, ignores michael@0: // its arguments (we instead parse GetCommandLineW() directly) because we michael@0: // don't trust the CRT's parsing of the command line, but it still must be michael@0: // called to set up the command line. Returns false if initialization has michael@0: // already occurred, and true otherwise. Only the caller receiving a 'true' michael@0: // return value should take responsibility for calling Reset. michael@0: static bool 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, as in main(), Reset() is not necessary. michael@0: static void Reset(); michael@0: michael@0: // Get the singleton CommandLine representing the current process's michael@0: // command line. Note: returned value is mutable, but not thread safe; michael@0: // only mutate if you know what you're doing! michael@0: static CommandLine* ForCurrentProcess(); michael@0: michael@0: // Returns true if the CommandLine has been initialized for the given process. michael@0: static bool InitializedForCurrentProcess(); michael@0: michael@0: #if defined(OS_WIN) michael@0: static CommandLine FromString(const std::wstring& command_line); michael@0: #endif michael@0: michael@0: // Initialize from an argv vector. michael@0: void InitFromArgv(int argc, const CharType* const* argv); michael@0: void InitFromArgv(const StringVector& argv); michael@0: michael@0: // Constructs and returns the represented command line string. michael@0: // CAUTION! This should be avoided on POSIX because quoting behavior is michael@0: // unclear. michael@0: StringType GetCommandLineString() const; michael@0: michael@0: // Constructs and returns the represented arguments string. michael@0: // CAUTION! This should be avoided on POSIX because quoting behavior is michael@0: // unclear. michael@0: StringType GetArgumentsString() const; michael@0: michael@0: // Returns the original command line string as a vector of strings. michael@0: const StringVector& argv() const { return argv_; } michael@0: michael@0: // Get and Set the program part of the command line string (the first item). michael@0: base::FilePath GetProgram() const; michael@0: void SetProgram(const base::FilePath& program); 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::string& switch_string) const; michael@0: michael@0: // Returns the value associated with the given switch. If the switch has no michael@0: // value or isn't present, this method returns the empty string. michael@0: std::string GetSwitchValueASCII(const std::string& switch_string) const; michael@0: base::FilePath GetSwitchValuePath(const std::string& switch_string) const; michael@0: StringType GetSwitchValueNative(const std::string& switch_string) const; michael@0: michael@0: // Get a copy of all switches, along with their values. michael@0: const SwitchMap& GetSwitches() const { return switches_; } michael@0: michael@0: // Append a switch [with optional value] to the command line. michael@0: // Note: Switches will precede arguments regardless of appending order. michael@0: void AppendSwitch(const std::string& switch_string); michael@0: void AppendSwitchPath(const std::string& switch_string, michael@0: const base::FilePath& path); michael@0: void AppendSwitchNative(const std::string& switch_string, michael@0: const StringType& value); michael@0: void AppendSwitchASCII(const std::string& switch_string, michael@0: const std::string& value); michael@0: michael@0: // Copy a set of switches (and any values) from another command line. michael@0: // Commonly used when launching a subprocess. michael@0: void CopySwitchesFrom(const CommandLine& source, michael@0: const char* const switches[], michael@0: size_t count); michael@0: michael@0: // Get the remaining arguments to the command. michael@0: StringVector GetArgs() const; michael@0: michael@0: // Append an argument to the command line. Note that the argument is quoted michael@0: // properly such that it is interpreted as one argument to the target command. michael@0: // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8. michael@0: // Note: Switches will precede arguments regardless of appending order. michael@0: void AppendArg(const std::string& value); michael@0: void AppendArgPath(const base::FilePath& value); michael@0: void AppendArgNative(const StringType& value); michael@0: michael@0: // Append the switches and 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, bool include_program); michael@0: michael@0: // Insert a command before the current command. michael@0: // Common for debuggers, like "valgrind" or "gdb --args". michael@0: void PrependWrapper(const StringType& wrapper); michael@0: michael@0: #if defined(OS_WIN) michael@0: // Initialize by parsing 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: #endif michael@0: michael@0: private: michael@0: // Disallow default constructor; a program name must be explicitly specified. michael@0: CommandLine(); michael@0: // Allow the copy constructor. A common pattern is to copy of the current michael@0: // process's command line and then add some flags to it. For example: michael@0: // CommandLine cl(*CommandLine::ForCurrentProcess()); michael@0: // cl.AppendSwitch(...); michael@0: michael@0: // The singleton CommandLine representing the current process's command line. michael@0: static CommandLine* current_process_commandline_; michael@0: michael@0: // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } michael@0: StringVector argv_; michael@0: michael@0: // Parsed-out switch keys and values. michael@0: SwitchMap switches_; michael@0: michael@0: // The index after the program and switches, any arguments start here. michael@0: size_t begin_args_; michael@0: }; michael@0: michael@0: #endif // BASE_COMMAND_LINE_H_