1 // Copyright 2014 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // This class works with command lines: building and parsing. 6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches. 7 // Switches will precede all other arguments without switch prefixes. 8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value". 9 // An argument of "--" will terminate switch parsing during initialization, 10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix. 11 12 // There is a singleton read-only CommandLine that represents the command line 13 // that the current process was started with. It must be initialized in main(). 14 15 #ifndef GESTURES_COMMAND_LINE_H_ 16 #define GESTURES_COMMAND_LINE_H_ 17 18 #include <stddef.h> 19 #include <map> 20 #include <string> 21 #include <vector> 22 23 namespace gestures { 24 25 class CommandLine { 26 public: 27 typedef std::string::value_type CharType; 28 typedef std::vector<std::string> StringVector; 29 typedef std::map<std::string, std::string> SwitchMap; 30 31 CommandLine(); 32 ~CommandLine(); 33 34 // Initialize the current process CommandLine singleton. On Windows, ignores 35 // its arguments (we instead parse GetCommandLineW() directly) because we 36 // don't trust the CRT's parsing of the command line, but it still must be 37 // called to set up the command line. Returns false if initialization has 38 // already occurred, and true otherwise. Only the caller receiving a 'true' 39 // return value should take responsibility for calling Reset. 40 static bool Init(int argc, const char* const* argv); 41 42 // Destroys the current process CommandLine singleton. This is necessary if 43 // you want to reset the base library to its initial state (for example, in an 44 // outer library that needs to be able to terminate, and be re-initialized). 45 // If Init is called only once, as in main(), Reset() is not necessary. 46 static void Reset(); 47 48 // Get the singleton CommandLine representing the current process's 49 // command line. Note: returned value is mutable, but not thread safe; 50 // only mutate if you know what you're doing! 51 static CommandLine* ForCurrentProcess(); 52 53 // Initialize from an argv vector. 54 void InitFromArgv(int argc, const CharType* const* argv); 55 void InitFromArgv(const StringVector& argv); 56 57 // Returns the original command line string as a vector of strings. argv()58 const StringVector& argv() const { return argv_; } 59 60 // Get and Set the program part of the command line string (the first item). 61 std::string GetProgram() const; 62 void SetProgram(const std::string& program); 63 64 // Returns true if this command line contains the given switch. 65 // (Switch names are case-insensitive). 66 bool HasSwitch(const std::string& switch_string) const; 67 68 // Returns the value associated with the given switch. If the switch has no 69 // value or isn't present, this method returns the empty string. 70 std::string GetSwitchValueASCII(const std::string& switch_string) const; 71 72 // Get a copy of all switches, along with their values. GetSwitches()73 const SwitchMap& GetSwitches() const { return switches_; } 74 75 // Append a switch [with optional value] to the command line. 76 // Note: Switches will precede arguments regardless of appending order. 77 void AppendSwitch(const std::string& switch_string); 78 void AppendSwitchASCII(const std::string& switch_string, 79 const std::string& value); 80 81 // Get the remaining arguments to the command. 82 StringVector GetArgs() const; 83 84 // Append an argument to the command line. Note that the argument is quoted 85 // properly such that it is interpreted as one argument to the target command. 86 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8. 87 // Note: Switches will precede arguments regardless of appending order. 88 void AppendArgASCII(const std::string& value); 89 90 private: 91 // Allow the copy constructor. A common pattern is to copy of the current 92 // process's command line and then add some flags to it. For example: 93 // CommandLine cl(*CommandLine::ForCurrentProcess()); 94 // cl.AppendSwitch(...); 95 96 // The singleton CommandLine representing the current process's command line. 97 static CommandLine* current_process_commandline_; 98 99 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } 100 StringVector argv_; 101 102 // Parsed-out switch keys and values. 103 SwitchMap switches_; 104 105 // The index after the program and switches, any arguments start here. 106 size_t begin_args_; 107 }; 108 109 } // namespace gestures 110 111 #endif // GESTURES_COMMAND_LINE_H_ 112