1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #ifndef _CMDLINE_HXX_
23 #define _CMDLINE_HXX_
24 
25 #include "defs.hxx"
26 
27 //---------------------------------
28 /** Simple command line abstraction
29 */
30 
31 class CommandLine
32 {
33 public:
34 
35 	//################################
36 	// Creation
37 	//################################
38 
39 
40 	CommandLine(size_t argc, char* argv[], const std::string& ArgPrefix = std::string("-"));
41 
42 
43 	//################################
44 	// Query
45 	//################################
46 
47 
48 	/** Return the argument count
49 	*/
50 	size_t get_arg_count() const;
51 
52 	/** Return an argument by index
53 		This method doesn't skip argument
54 		names if any, so if the second
55 		argument is an argument name the
56 		function nevertheless returns it.
57 
58 		@precond	0 <= Index < GetArgumentCount
59 
60 		@throws std::out_of_range exception
61 		if the given index is to high
62 	*/
63 	std::string get_arg(size_t Index) const;
64 
65 	/** Returns all argument name found in the
66 		command line. An argument will be identified
67 		by a specified prefix. The standard prefix
68 		is '-'.
69 		If there are no argument names the returned
70 		container is empty.
71 	*/
72 	StringListPtr_t get_arg_names() const;
73 
74 	/** Returns an argument by name. If there are
75 		duplicate argument names in the command line,
76 		the first one wins.
77 		Argument name an the argument value must be separated
78 		by spaces. If the argument value starts with an
79 		argument prefix use quotes else the return value is
80 		an empty string because the value will be interpreted
81 		as the next argument name.
82 		If an argument value contains spaces use quotes.
83 
84 		@precond	GetArgumentNames() -> has element ArgumentName
85 
86 		@throws std::invalid_argument exception
87 		if the specified argument could not be
88 		found
89 	*/
90 	std::string get_arg(const std::string& ArgumentName) const;
91 
92 
93 	//################################
94 	// Command
95 	//################################
96 
97 
98 	/** Set the prefix used to identify arguments in
99 		the command line.
100 
101 		@precond	prefix is not empty
102 
103 		@throws std::invalid_argument exception if
104 		the prefix is empty
105 	*/
106 	void set_arg_prefix(const std::string& Prefix);
107 
108 private:
109 
110 	/** Returns whether a given argument is an argument name
111 	*/
112 	bool is_arg_name(const std::string& Argument) const;
113 
114 private:
115 	size_t		m_argc;
116 	char**		m_argv;
117 	std::string m_argprefix;
118 
119 // prevent copy and assignment
120 private:
121 	CommandLine(const CommandLine&);
122 	CommandLine& operator=(const CommandLine&);
123 };
124 
125 #endif
126