[go: nahoru, domu]

blob: 9a02546b204864aa3a9b5631fd56c1f41d1acac3 [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.
deanm@chromium.org02c87962008-10-06 10:25:354
evan@chromium.orgbb975362009-01-21 01:00:225// This class works with command lines: building and parsing.
initial.commitd7cae122008-07-26 21:49:386// Switches can optionally have a value attached using an equals sign,
7// as in "-switch=value". Arguments that aren't prefixed with a
evan@chromium.orgbb975362009-01-21 01:00:228// switch prefix are considered "loose parameters". Switch names are
9// case-insensitive. An argument of "--" will terminate switch
10// parsing, causing everything after to be considered as loose
11// parameters.
12
13// There is a singleton read-only CommandLine that represents the command
14// line that the current process was started with. It must be initialized
15// in main() (or whatever the platform's equivalent function is).
initial.commitd7cae122008-07-26 21:49:3816
deanm@chromium.org02c87962008-10-06 10:25:3517#ifndef BASE_COMMAND_LINE_H_
18#define BASE_COMMAND_LINE_H_
evan@chromium.orgbb975362009-01-21 01:00:2219
20#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3821
22#include <map>
23#include <string>
24#include <vector>
25
26#include "base/basictypes.h"
evan@chromium.orgbb975362009-01-21 01:00:2227#include "base/logging.h"
initial.commitd7cae122008-07-26 21:49:3828#include "base/scoped_ptr.h"
29
sky@google.comd4515eb2009-01-30 00:40:4330class InProcessBrowserTest;
31
initial.commitd7cae122008-07-26 21:49:3832class CommandLine {
33 public:
evanm@google.comf3adb5c2008-08-07 20:07:3234#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3835 // Creates a parsed version of the given command-line string.
evan@chromium.orgbb975362009-01-21 01:00:2236 // The program name is assumed to be the first item in the string.
37 void ParseFromString(const std::wstring& command_line);
evanm@google.comf3adb5c2008-08-07 20:07:3238#elif defined(OS_POSIX)
evan@chromium.orgbb975362009-01-21 01:00:2239 // Initialize from an argv vector (or directly from main()'s argv).
tc@google.come63d5982008-08-14 22:09:3940 CommandLine(int argc, const char* const* argv);
evan@chromium.orgbb975362009-01-21 01:00:2241 explicit CommandLine(const std::vector<std::string>& argv);
evanm@google.comf3adb5c2008-08-07 20:07:3242#endif
initial.commitd7cae122008-07-26 21:49:3843
evan@chromium.orgbb975362009-01-21 01:00:2244 // Construct a new, empty command line.
45 // |program| is the name of the program to run (aka argv[0]).
46 // TODO(port): should be a FilePath.
47 explicit CommandLine(const std::wstring& program);
initial.commitd7cae122008-07-26 21:49:3848
evan@chromium.orgbb975362009-01-21 01:00:2249 // Initialize the current process CommandLine singleton. On Windows,
50 // ignores its arguments (we instead parse GetCommandLineW()
51 // directly) because we don't trust the CRT's parsing of the command
52 // line, but it still must be called to set up the command line.
53 static void Init(int argc, const char* const* argv);
54
55 // Get the singleton CommandLine representing the current process's
56 // command line.
57 static const CommandLine* ForCurrentProcess() {
58 DCHECK(current_process_commandline_);
59 return current_process_commandline_;
60 }
evanm@google.com1a48f312008-08-12 01:14:3761
initial.commitd7cae122008-07-26 21:49:3862 // Returns true if this command line contains the given switch.
63 // (Switch names are case-insensitive.)
64 bool HasSwitch(const std::wstring& switch_string) const;
65
66 // Returns the value associated with the given switch. If the
67 // switch has no value or isn't present, this method returns
68 // the empty string.
69 std::wstring GetSwitchValue(const std::wstring& switch_string) const;
70
evan@chromium.orgbb975362009-01-21 01:00:2271 // Get the remaining arguments to the command.
72 // WARNING: this is incorrect on POSIX; we must do string conversions.
73 std::vector<std::wstring> GetLooseValues() const;
initial.commitd7cae122008-07-26 21:49:3874
evan@chromium.orgbb975362009-01-21 01:00:2275#if defined(OS_WIN)
76 // Returns the original command line string.
77 const std::wstring& command_line_string() const {
78 return command_line_string_;
79 }
80#elif defined(OS_POSIX)
estade@chromium.org10e42bf2008-10-15 21:59:0881 // Returns the original command line string as a vector of strings.
evan@chromium.orgbb975362009-01-21 01:00:2282 const std::vector<std::string>& argv() const {
83 return argv_;
84 }
estade@chromium.org10e42bf2008-10-15 21:59:0885#endif
86
initial.commitd7cae122008-07-26 21:49:3887 // Returns the program part of the command line string (the first item).
88 std::wstring program() const;
deanm@chromium.org02c87962008-10-06 10:25:3589
estade@chromium.org10e42bf2008-10-15 21:59:0890 // Return a copy of the string prefixed with a switch prefix.
91 // Used internally.
92 static std::wstring PrefixedSwitchString(const std::wstring& switch_string);
93
94 // Return a copy of the string prefixed with a switch prefix,
95 // and appended with the given value. Used internally.
96 static std::wstring PrefixedSwitchStringWithValue(
97 const std::wstring& switch_string,
98 const std::wstring& value_string);
99
initial.commitd7cae122008-07-26 21:49:38100 // Appends the given switch string (preceded by a space and a switch
101 // prefix) to the given string.
evan@chromium.orgbb975362009-01-21 01:00:22102 void AppendSwitch(const std::wstring& switch_string);
initial.commitd7cae122008-07-26 21:49:38103
104 // Appends the given switch string (preceded by a space and a switch
105 // prefix) to the given string, with the given value attached.
evan@chromium.orgbb975362009-01-21 01:00:22106 void AppendSwitchWithValue(const std::wstring& switch_string,
107 const std::wstring& value_string);
108
109 // Append a loose value to the command line.
110 void AppendLooseValue(const std::wstring& value);
111
112 // Append the arguments from another command line to this one.
113 // If |include_program| is true, include |other|'s program as well.
114 void AppendArguments(const CommandLine& other,
115 bool include_program);
initial.commitd7cae122008-07-26 21:49:38116
117 private:
sky@google.comd4515eb2009-01-30 00:40:43118 friend class InProcessBrowserTest;
119
evan@chromium.orgbb975362009-01-21 01:00:22120 CommandLine() {}
initial.commitd7cae122008-07-26 21:49:38121
sky@google.comd4515eb2009-01-30 00:40:43122 // Used by InProcessBrowserTest.
123 static CommandLine* ForCurrentProcessMutable() {
124 DCHECK(current_process_commandline_);
125 return current_process_commandline_;
126 }
127
evan@chromium.orgbb975362009-01-21 01:00:22128 // The singleton CommandLine instance representing the current process's
129 // command line.
130 static CommandLine* current_process_commandline_;
initial.commitd7cae122008-07-26 21:49:38131
evan@chromium.orgbb975362009-01-21 01:00:22132 // We store a platform-native version of the command line, used when building
133 // up a new command line to be executed. This ifdef delimits that code.
134
135#if defined(OS_WIN)
136 // The quoted, space-separated command-line string.
137 std::wstring command_line_string_;
138
139 // The name of the program.
140 std::wstring program_;
141
142 // The type of native command line arguments.
143 typedef std::wstring StringType;
144
145#elif defined(OS_POSIX)
146 // The argv array, with the program name in argv_[0].
147 std::vector<std::string> argv_;
148
149 // The type of native command line arguments.
150 typedef std::string StringType;
151
152 // Shared by the two POSIX constructor forms. Initalize from argv_.
153 void InitFromArgv();
154#endif
155
156 // Returns true and fills in |switch_string| and |switch_value|
157 // if |parameter_string| represents a switch.
158 static bool IsSwitch(const StringType& parameter_string,
159 std::string* switch_string,
160 StringType* switch_value);
161
162 // Parsed-out values.
163 std::map<std::string, StringType> switches_;
164
165 // Non-switch command-line arguments.
166 std::vector<StringType> loose_values_;
167
168 // We allow copy constructors, because a common pattern is to grab a
169 // copy of the current process's command line and then add some
170 // flags to it. E.g.:
171 // CommandLine cl(*CommandLine::ForCurrentProcess());
172 // cl.AppendSwitch(...);
initial.commitd7cae122008-07-26 21:49:38173};
174
deanm@chromium.org02c87962008-10-06 10:25:35175#endif // BASE_COMMAND_LINE_H_