license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [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. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | // |
| 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 |
| 10 | // are case-insensitive. |
| 11 | |
| 12 | #ifndef BASE_COMMAND_LINE_H__ |
| 13 | #define BASE_COMMAND_LINE_H__ |
| 14 | |
| 15 | #include <map> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "base/basictypes.h" |
| 20 | #include "base/scoped_ptr.h" |
| 21 | |
| 22 | class CommandLine { |
| 23 | public: |
| 24 | // Creates a parsed version of the command line used to launch |
| 25 | // the current process. |
| 26 | CommandLine(); |
| 27 | |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 28 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 29 | // Creates a parsed version of the given command-line string. |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 30 | // The program name is assumed to be the first item in the string. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 31 | CommandLine(const std::wstring& command_line); |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 32 | #elif defined(OS_POSIX) |
tc@google.com | 1bc8d52 | 2008-08-15 07:09:39 +0900 | [diff] [blame] | 33 | CommandLine(int argc, const char* const* argv); |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 34 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 35 | |
| 36 | ~CommandLine(); |
| 37 | |
evanm@google.com | 638e9fb | 2008-08-12 10:14:37 +0900 | [diff] [blame] | 38 | // On non-Windows platforms, main() must call SetArgcArgv() before accessing |
| 39 | // any members of this class. |
| 40 | // On Windows, this call is a no-op (we instead parse GetCommandLineW() |
| 41 | // directly) because we don't trust the CRT's parsing of the command line. |
tc@google.com | 1bc8d52 | 2008-08-15 07:09:39 +0900 | [diff] [blame] | 42 | static void SetArgcArgv(int argc, const char* const* argv); |
evanm@google.com | 638e9fb | 2008-08-12 10:14:37 +0900 | [diff] [blame] | 43 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 44 | // Returns true if this command line contains the given switch. |
| 45 | // (Switch names are case-insensitive.) |
| 46 | bool HasSwitch(const std::wstring& switch_string) const; |
| 47 | |
| 48 | // Returns the value associated with the given switch. If the |
| 49 | // switch has no value or isn't present, this method returns |
| 50 | // the empty string. |
| 51 | std::wstring GetSwitchValue(const std::wstring& switch_string) const; |
| 52 | |
| 53 | // Returns the number of "loose values" found in the command line. |
| 54 | // Loose values are arguments that aren't switches. |
| 55 | // (The program name is also excluded from the set of loose values.) |
| 56 | size_t GetLooseValueCount() const; |
| 57 | |
| 58 | typedef std::vector<std::wstring>::const_iterator LooseValueIterator; |
| 59 | |
| 60 | // Returns a const_iterator to the list of loose values. |
| 61 | LooseValueIterator GetLooseValuesBegin() const; |
| 62 | |
| 63 | // Returns the end const_iterator for the list of loose values. |
| 64 | LooseValueIterator GetLooseValuesEnd() const; |
| 65 | |
| 66 | // Simply returns the original command line string. |
| 67 | std::wstring command_line_string() const; |
| 68 | |
| 69 | // Returns the program part of the command line string (the first item). |
| 70 | std::wstring program() const; |
| 71 | |
| 72 | // An array containing the prefixes that identify an argument as |
| 73 | // a switch. |
| 74 | static const wchar_t* const kSwitchPrefixes[]; |
| 75 | |
| 76 | // The string that's used to separate switches from their values. |
| 77 | static const wchar_t kSwitchValueSeparator[]; |
| 78 | |
| 79 | // Appends the given switch string (preceded by a space and a switch |
| 80 | // prefix) to the given string. |
| 81 | static void AppendSwitch(std::wstring* command_line_string, |
| 82 | const std::wstring& switch_string); |
| 83 | |
| 84 | // Appends the given switch string (preceded by a space and a switch |
| 85 | // prefix) to the given string, with the given value attached. |
| 86 | static void AppendSwitchWithValue(std::wstring* command_line_string, |
| 87 | const std::wstring& switch_string, |
| 88 | const std::wstring& value_string); |
| 89 | |
| 90 | private: |
| 91 | class Data; |
| 92 | |
| 93 | // True if we are responsible for deleting our |data_| pointer. In some cases |
| 94 | // we cache the result of parsing the command line and |data_|'s lifetime is |
| 95 | // managed by someone else (e.g., the |Singleton| class). |
| 96 | bool we_own_data_; |
| 97 | |
| 98 | // A pointer to the parsed version of the command line. |
| 99 | Data* data_; |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 100 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 101 | DISALLOW_EVIL_CONSTRUCTORS(CommandLine); |
| 102 | }; |
| 103 | |
| 104 | #endif // BASE_COMMAND_LINE_H__ |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 105 | |