initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // This file contains a class that can be used to extract the salient |
| 31 | // elements of a command line in a relatively lightweight manner. |
| 32 | // Switches can optionally have a value attached using an equals sign, |
| 33 | // as in "-switch=value". Arguments that aren't prefixed with a |
| 34 | // switch prefix are considered "loose parameters". Switch names |
| 35 | // are case-insensitive. |
| 36 | |
| 37 | #ifndef BASE_COMMAND_LINE_H__ |
| 38 | #define BASE_COMMAND_LINE_H__ |
| 39 | |
| 40 | #include <map> |
| 41 | #include <string> |
| 42 | #include <vector> |
| 43 | |
| 44 | #include "base/basictypes.h" |
| 45 | #include "base/scoped_ptr.h" |
| 46 | |
| 47 | class CommandLine { |
| 48 | public: |
| 49 | // Creates a parsed version of the command line used to launch |
| 50 | // the current process. |
| 51 | CommandLine(); |
| 52 | |
evanm@google.com | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame^] | 53 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 54 | // Creates a parsed version of the given command-line string. |
evanm@google.com | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame^] | 55 | // The program name is assumed to be the first item in the string. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 56 | CommandLine(const std::wstring& command_line); |
evanm@google.com | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame^] | 57 | #elif defined(OS_POSIX) |
| 58 | CommandLine(int argc, const char** argv); |
| 59 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 60 | |
| 61 | ~CommandLine(); |
| 62 | |
| 63 | // Returns true if this command line contains the given switch. |
| 64 | // (Switch names are case-insensitive.) |
| 65 | bool HasSwitch(const std::wstring& switch_string) const; |
| 66 | |
| 67 | // Returns the value associated with the given switch. If the |
| 68 | // switch has no value or isn't present, this method returns |
| 69 | // the empty string. |
| 70 | std::wstring GetSwitchValue(const std::wstring& switch_string) const; |
| 71 | |
| 72 | // Returns the number of "loose values" found in the command line. |
| 73 | // Loose values are arguments that aren't switches. |
| 74 | // (The program name is also excluded from the set of loose values.) |
| 75 | size_t GetLooseValueCount() const; |
| 76 | |
| 77 | typedef std::vector<std::wstring>::const_iterator LooseValueIterator; |
| 78 | |
| 79 | // Returns a const_iterator to the list of loose values. |
| 80 | LooseValueIterator GetLooseValuesBegin() const; |
| 81 | |
| 82 | // Returns the end const_iterator for the list of loose values. |
| 83 | LooseValueIterator GetLooseValuesEnd() const; |
| 84 | |
| 85 | // Simply returns the original command line string. |
| 86 | std::wstring command_line_string() const; |
| 87 | |
| 88 | // Returns the program part of the command line string (the first item). |
| 89 | std::wstring program() const; |
| 90 | |
| 91 | // An array containing the prefixes that identify an argument as |
| 92 | // a switch. |
| 93 | static const wchar_t* const kSwitchPrefixes[]; |
| 94 | |
| 95 | // The string that's used to separate switches from their values. |
| 96 | static const wchar_t kSwitchValueSeparator[]; |
| 97 | |
| 98 | // Appends the given switch string (preceded by a space and a switch |
| 99 | // prefix) to the given string. |
| 100 | static void AppendSwitch(std::wstring* command_line_string, |
| 101 | const std::wstring& switch_string); |
| 102 | |
| 103 | // Appends the given switch string (preceded by a space and a switch |
| 104 | // prefix) to the given string, with the given value attached. |
| 105 | static void AppendSwitchWithValue(std::wstring* command_line_string, |
| 106 | const std::wstring& switch_string, |
| 107 | const std::wstring& value_string); |
| 108 | |
| 109 | private: |
| 110 | class Data; |
| 111 | |
| 112 | // True if we are responsible for deleting our |data_| pointer. In some cases |
| 113 | // we cache the result of parsing the command line and |data_|'s lifetime is |
| 114 | // managed by someone else (e.g., the |Singleton| class). |
| 115 | bool we_own_data_; |
| 116 | |
| 117 | // A pointer to the parsed version of the command line. |
| 118 | Data* data_; |
evanm@google.com | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame^] | 119 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 120 | DISALLOW_EVIL_CONSTRUCTORS(CommandLine); |
| 121 | }; |
| 122 | |
| 123 | #endif // BASE_COMMAND_LINE_H__ |