[go: nahoru, domu]

blob: f4f3843151177c616dd8421460e1dab4804664bc [file] [log] [blame]
initial.commitd7cae122008-07-26 21:49:381// 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
47class CommandLine {
48 public:
49 // Creates a parsed version of the command line used to launch
50 // the current process.
51 CommandLine();
52
evanm@google.comf3adb5c2008-08-07 20:07:3253#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3854 // Creates a parsed version of the given command-line string.
evanm@google.comf3adb5c2008-08-07 20:07:3255 // The program name is assumed to be the first item in the string.
initial.commitd7cae122008-07-26 21:49:3856 CommandLine(const std::wstring& command_line);
evanm@google.comf3adb5c2008-08-07 20:07:3257#elif defined(OS_POSIX)
tc@google.come63d5982008-08-14 22:09:3958 CommandLine(int argc, const char* const* argv);
evanm@google.comf3adb5c2008-08-07 20:07:3259#endif
initial.commitd7cae122008-07-26 21:49:3860
61 ~CommandLine();
62
evanm@google.com1a48f312008-08-12 01:14:3763 // On non-Windows platforms, main() must call SetArgcArgv() before accessing
64 // any members of this class.
65 // On Windows, this call is a no-op (we instead parse GetCommandLineW()
66 // directly) because we don't trust the CRT's parsing of the command line.
tc@google.come63d5982008-08-14 22:09:3967 static void SetArgcArgv(int argc, const char* const* argv);
evanm@google.com1a48f312008-08-12 01:14:3768
initial.commitd7cae122008-07-26 21:49:3869 // Returns true if this command line contains the given switch.
70 // (Switch names are case-insensitive.)
71 bool HasSwitch(const std::wstring& switch_string) const;
72
73 // Returns the value associated with the given switch. If the
74 // switch has no value or isn't present, this method returns
75 // the empty string.
76 std::wstring GetSwitchValue(const std::wstring& switch_string) const;
77
78 // Returns the number of "loose values" found in the command line.
79 // Loose values are arguments that aren't switches.
80 // (The program name is also excluded from the set of loose values.)
81 size_t GetLooseValueCount() const;
82
83 typedef std::vector<std::wstring>::const_iterator LooseValueIterator;
84
85 // Returns a const_iterator to the list of loose values.
86 LooseValueIterator GetLooseValuesBegin() const;
87
88 // Returns the end const_iterator for the list of loose values.
89 LooseValueIterator GetLooseValuesEnd() const;
90
91 // Simply returns the original command line string.
92 std::wstring command_line_string() const;
93
94 // Returns the program part of the command line string (the first item).
95 std::wstring program() const;
96
97 // An array containing the prefixes that identify an argument as
98 // a switch.
99 static const wchar_t* const kSwitchPrefixes[];
100
101 // The string that's used to separate switches from their values.
102 static const wchar_t kSwitchValueSeparator[];
103
104 // Appends the given switch string (preceded by a space and a switch
105 // prefix) to the given string.
106 static void AppendSwitch(std::wstring* command_line_string,
107 const std::wstring& switch_string);
108
109 // Appends the given switch string (preceded by a space and a switch
110 // prefix) to the given string, with the given value attached.
111 static void AppendSwitchWithValue(std::wstring* command_line_string,
112 const std::wstring& switch_string,
113 const std::wstring& value_string);
114
115 private:
116 class Data;
117
118 // True if we are responsible for deleting our |data_| pointer. In some cases
119 // we cache the result of parsing the command line and |data_|'s lifetime is
120 // managed by someone else (e.g., the |Singleton| class).
121 bool we_own_data_;
122
123 // A pointer to the parsed version of the command line.
124 Data* data_;
evanm@google.comf3adb5c2008-08-07 20:07:32125
initial.commitd7cae122008-07-26 21:49:38126 DISALLOW_EVIL_CONSTRUCTORS(CommandLine);
127};
128
129#endif // BASE_COMMAND_LINE_H__