blob: 56aa2fc8f5808f9f28d78bdbac5611bb373b4830 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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]02c87962008-10-06 10:25:354
initial.commitd7cae122008-07-26 21:49:385// 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]02c87962008-10-06 10:25:3510// are case-insensitive. An argument of "--" will terminate switch parsing,
11// causing everything after to be considered as loose parameters.
initial.commitd7cae122008-07-26 21:49:3812
[email protected]02c87962008-10-06 10:25:3513#ifndef BASE_COMMAND_LINE_H_
14#define BASE_COMMAND_LINE_H_
initial.commitd7cae122008-07-26 21:49:3815
16#include <map>
17#include <string>
18#include <vector>
19
20#include "base/basictypes.h"
21#include "base/scoped_ptr.h"
22
23class CommandLine {
24 public:
25 // Creates a parsed version of the command line used to launch
26 // the current process.
27 CommandLine();
28
[email protected]f3adb5c2008-08-07 20:07:3229#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3830 // Creates a parsed version of the given command-line string.
[email protected]f3adb5c2008-08-07 20:07:3231 // The program name is assumed to be the first item in the string.
initial.commitd7cae122008-07-26 21:49:3832 CommandLine(const std::wstring& command_line);
[email protected]f3adb5c2008-08-07 20:07:3233#elif defined(OS_POSIX)
[email protected]e63d5982008-08-14 22:09:3934 CommandLine(int argc, const char* const* argv);
[email protected]10e42bf2008-10-15 21:59:0835 CommandLine(const std::vector<std::string>& argv);
[email protected]f3adb5c2008-08-07 20:07:3236#endif
initial.commitd7cae122008-07-26 21:49:3837
38 ~CommandLine();
39
[email protected]1a48f312008-08-12 01:14:3740 // 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]e63d5982008-08-14 22:09:3944 static void SetArgcArgv(int argc, const char* const* argv);
[email protected]1a48f312008-08-12 01:14:3745
initial.commitd7cae122008-07-26 21:49:3846 // 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]10e42bf2008-10-15 21:59:0871#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.commitd7cae122008-07-26 21:49:3876 // 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]02c87962008-10-06 10:25:3586 // Treat everything after this argument as loose parameters.
87 static const wchar_t kSwitchTerminator[];
88
[email protected]10e42bf2008-10-15 21:59:0889 // 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.commitd7cae122008-07-26 21:49:3899 // 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]f3adb5c2008-08-07 20:07:32120
initial.commitd7cae122008-07-26 21:49:38121 DISALLOW_EVIL_CONSTRUCTORS(CommandLine);
122};
123
[email protected]02c87962008-10-06 10:25:35124#endif // BASE_COMMAND_LINE_H_