license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 4 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 5 | // This file contains a class that can be used to extract the salient |
| 6 | // elements of a command line in a relatively lightweight manner. |
| 7 | // Switches can optionally have a value attached using an equals sign, |
| 8 | // as in "-switch=value". Arguments that aren't prefixed with a |
| 9 | // switch prefix are considered "loose parameters". Switch names |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 10 | // are case-insensitive. An argument of "--" will terminate switch parsing, |
| 11 | // causing everything after to be considered as loose parameters. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 13 | #ifndef BASE_COMMAND_LINE_H_ |
| 14 | #define BASE_COMMAND_LINE_H_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 15 | |
| 16 | #include <map> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "base/basictypes.h" |
| 21 | #include "base/scoped_ptr.h" |
| 22 | |
| 23 | class CommandLine { |
| 24 | public: |
| 25 | // Creates a parsed version of the command line used to launch |
| 26 | // the current process. |
| 27 | CommandLine(); |
| 28 | |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 29 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | // Creates a parsed version of the given command-line string. |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 31 | // The program name is assumed to be the first item in the string. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 32 | CommandLine(const std::wstring& command_line); |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 33 | #elif defined(OS_POSIX) |
[email protected] | e63d598 | 2008-08-14 22:09:39 | [diff] [blame] | 34 | CommandLine(int argc, const char* const* argv); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 35 | CommandLine(const std::vector<std::string>& argv); |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 36 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 37 | |
| 38 | ~CommandLine(); |
| 39 | |
[email protected] | 1a48f31 | 2008-08-12 01:14:37 | [diff] [blame] | 40 | // On non-Windows platforms, main() must call SetArgcArgv() before accessing |
| 41 | // any members of this class. |
| 42 | // On Windows, this call is a no-op (we instead parse GetCommandLineW() |
| 43 | // directly) because we don't trust the CRT's parsing of the command line. |
[email protected] | e63d598 | 2008-08-14 22:09:39 | [diff] [blame] | 44 | static void SetArgcArgv(int argc, const char* const* argv); |
[email protected] | 1a48f31 | 2008-08-12 01:14:37 | [diff] [blame] | 45 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 46 | // Returns true if this command line contains the given switch. |
| 47 | // (Switch names are case-insensitive.) |
| 48 | bool HasSwitch(const std::wstring& switch_string) const; |
| 49 | |
| 50 | // Returns the value associated with the given switch. If the |
| 51 | // switch has no value or isn't present, this method returns |
| 52 | // the empty string. |
| 53 | std::wstring GetSwitchValue(const std::wstring& switch_string) const; |
| 54 | |
| 55 | // Returns the number of "loose values" found in the command line. |
| 56 | // Loose values are arguments that aren't switches. |
| 57 | // (The program name is also excluded from the set of loose values.) |
| 58 | size_t GetLooseValueCount() const; |
| 59 | |
| 60 | typedef std::vector<std::wstring>::const_iterator LooseValueIterator; |
| 61 | |
| 62 | // Returns a const_iterator to the list of loose values. |
| 63 | LooseValueIterator GetLooseValuesBegin() const; |
| 64 | |
| 65 | // Returns the end const_iterator for the list of loose values. |
| 66 | LooseValueIterator GetLooseValuesEnd() const; |
| 67 | |
| 68 | // Simply returns the original command line string. |
| 69 | std::wstring command_line_string() const; |
| 70 | |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 71 | #if defined(OS_POSIX) |
| 72 | // Returns the original command line string as a vector of strings. |
| 73 | const std::vector<std::string>& argv() const; |
| 74 | #endif |
| 75 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 76 | // Returns the program part of the command line string (the first item). |
| 77 | std::wstring program() const; |
| 78 | |
| 79 | // An array containing the prefixes that identify an argument as |
| 80 | // a switch. |
| 81 | static const wchar_t* const kSwitchPrefixes[]; |
| 82 | |
| 83 | // The string that's used to separate switches from their values. |
| 84 | static const wchar_t kSwitchValueSeparator[]; |
| 85 | |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 86 | // Treat everything after this argument as loose parameters. |
| 87 | static const wchar_t kSwitchTerminator[]; |
| 88 | |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 89 | // Return a copy of the string prefixed with a switch prefix. |
| 90 | // Used internally. |
| 91 | static std::wstring PrefixedSwitchString(const std::wstring& switch_string); |
| 92 | |
| 93 | // Return a copy of the string prefixed with a switch prefix, |
| 94 | // and appended with the given value. Used internally. |
| 95 | static std::wstring PrefixedSwitchStringWithValue( |
| 96 | const std::wstring& switch_string, |
| 97 | const std::wstring& value_string); |
| 98 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 99 | // Appends the given switch string (preceded by a space and a switch |
| 100 | // prefix) to the given string. |
| 101 | static void AppendSwitch(std::wstring* command_line_string, |
| 102 | const std::wstring& switch_string); |
| 103 | |
| 104 | // Appends the given switch string (preceded by a space and a switch |
| 105 | // prefix) to the given string, with the given value attached. |
| 106 | static void AppendSwitchWithValue(std::wstring* command_line_string, |
| 107 | const std::wstring& switch_string, |
| 108 | const std::wstring& value_string); |
| 109 | |
| 110 | private: |
| 111 | class Data; |
| 112 | |
| 113 | // True if we are responsible for deleting our |data_| pointer. In some cases |
| 114 | // we cache the result of parsing the command line and |data_|'s lifetime is |
| 115 | // managed by someone else (e.g., the |Singleton| class). |
| 116 | bool we_own_data_; |
| 117 | |
| 118 | // A pointer to the parsed version of the command line. |
| 119 | Data* data_; |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 120 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 121 | DISALLOW_EVIL_CONSTRUCTORS(CommandLine); |
| 122 | }; |
| 123 | |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 124 | #endif // BASE_COMMAND_LINE_H_ |