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