1*f8e2c85aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f8e2c85aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f8e2c85aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f8e2c85aSAndrew Rist  * distributed with this work for additional information
6*f8e2c85aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f8e2c85aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f8e2c85aSAndrew Rist  * "License"); you may not use this file except in compliance
9*f8e2c85aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f8e2c85aSAndrew Rist  *
11*f8e2c85aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f8e2c85aSAndrew Rist  *
13*f8e2c85aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f8e2c85aSAndrew Rist  * software distributed under the License is distributed on an
15*f8e2c85aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f8e2c85aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*f8e2c85aSAndrew Rist  * specific language governing permissions and limitations
18*f8e2c85aSAndrew Rist  * under the License.
19*f8e2c85aSAndrew Rist  *
20*f8e2c85aSAndrew Rist  *************************************************************/
21*f8e2c85aSAndrew Rist 
22*f8e2c85aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
26cdf0e10cSrcweir #include "precompiled_shell.hxx"
27cdf0e10cSrcweir #include <stdexcept>
28cdf0e10cSrcweir #include <osl/diagnose.h>
29cdf0e10cSrcweir #include "cmdline.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir //---------------------------------
32cdf0e10cSrcweir /** Simple command line abstraction
33cdf0e10cSrcweir */
34cdf0e10cSrcweir 
35cdf0e10cSrcweir //################################
36cdf0e10cSrcweir // Creation
37cdf0e10cSrcweir //################################
38cdf0e10cSrcweir 
39cdf0e10cSrcweir 
CommandLine(size_t argc,char * argv[],const std::string & ArgPrefix)40cdf0e10cSrcweir CommandLine::CommandLine(size_t argc, char* argv[], const std::string& ArgPrefix) :
41cdf0e10cSrcweir 	m_argc(argc),
42cdf0e10cSrcweir 	m_argv(argv),
43cdf0e10cSrcweir 	m_argprefix(ArgPrefix)
44cdf0e10cSrcweir {
45cdf0e10cSrcweir }
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //################################
49cdf0e10cSrcweir // Query
50cdf0e10cSrcweir //################################
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 
53cdf0e10cSrcweir /** Return the argument count
54cdf0e10cSrcweir */
get_arg_count() const55cdf0e10cSrcweir size_t CommandLine::get_arg_count() const
56cdf0e10cSrcweir {
57cdf0e10cSrcweir 	return m_argc;
58cdf0e10cSrcweir }
59cdf0e10cSrcweir 
60cdf0e10cSrcweir /** Return an argument by index
61cdf0e10cSrcweir 	This method doesn't skip argument
62cdf0e10cSrcweir 	names if any, so if the second
63cdf0e10cSrcweir 	argument is an argument name the
64cdf0e10cSrcweir 	function nevertheless returns it.
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 	@precond	0 <= Index < GetArgumentCount
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	@throws std::out_of_range exception
69cdf0e10cSrcweir 	if the given index is to high
70cdf0e10cSrcweir */
get_arg(size_t Index) const71cdf0e10cSrcweir std::string CommandLine::get_arg(size_t Index) const
72cdf0e10cSrcweir {
73cdf0e10cSrcweir 	OSL_PRECOND(Index < m_argc, "Index out of range");
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	if (Index > (m_argc - 1))
76cdf0e10cSrcweir 		throw std::out_of_range("Invalid index");
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 	return m_argv[Index];
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 
82cdf0e10cSrcweir /** Returns all argument name found in the
83cdf0e10cSrcweir 	command line. An argument will be identified
84cdf0e10cSrcweir 	by a specified prefix. The standard prefix
85cdf0e10cSrcweir 	is '-'.
86cdf0e10cSrcweir 	If the are no argument names the returned
87cdf0e10cSrcweir 	container is empty.
88cdf0e10cSrcweir */
get_arg_names() const89cdf0e10cSrcweir StringListPtr_t CommandLine::get_arg_names() const
90cdf0e10cSrcweir {
91cdf0e10cSrcweir 	StringListPtr_t arg_cont(new StringList_t());
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 	for (size_t i = 0; i < m_argc; i++)
94cdf0e10cSrcweir 	{
95cdf0e10cSrcweir 		std::string argn = m_argv[i];
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 		if (is_arg_name(argn))
98cdf0e10cSrcweir 			arg_cont->push_back(argn);
99cdf0e10cSrcweir 	}
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 	return arg_cont;
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir /** Returns an argument by name. If there are
105cdf0e10cSrcweir 	duplicate argument names in the command line,
106cdf0e10cSrcweir 	the first one wins.
107cdf0e10cSrcweir 	Argument name an the argument value must be separated
108cdf0e10cSrcweir 	by spaces. If the argument value starts with an
109cdf0e10cSrcweir 	argument prefix use quotes else the return value is
110cdf0e10cSrcweir 	an empty string because the value will be interpreted
111cdf0e10cSrcweir 	as the next argument name.
112cdf0e10cSrcweir 	If an argument value contains spaces use quotes.
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	@precond	GetArgumentNames() -> has element ArgumentName
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	@throws std::invalid_argument exception
117cdf0e10cSrcweir 	if the specified argument could not be
118cdf0e10cSrcweir 	found
119cdf0e10cSrcweir */
get_arg(const std::string & ArgumentName) const120cdf0e10cSrcweir std::string CommandLine::get_arg(const std::string& ArgumentName) const
121cdf0e10cSrcweir {
122cdf0e10cSrcweir 	std::string arg_value;
123cdf0e10cSrcweir 	size_t i;
124cdf0e10cSrcweir 	for ( i = 0; i < m_argc; i++)
125cdf0e10cSrcweir 	{
126cdf0e10cSrcweir 		std::string arg = m_argv[i];
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 		if (ArgumentName == arg && ((i+1) < m_argc) && !is_arg_name(m_argv[i+1]))
129cdf0e10cSrcweir 		{
130cdf0e10cSrcweir 			arg_value = m_argv[i+1];
131cdf0e10cSrcweir 			break;
132cdf0e10cSrcweir 		}
133cdf0e10cSrcweir 	}
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 	if (i == m_argc)
136cdf0e10cSrcweir 		throw std::invalid_argument("Invalid argument name");
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 	return arg_value;
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 
142cdf0e10cSrcweir //################################
143cdf0e10cSrcweir // Command
144cdf0e10cSrcweir //################################
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 
147cdf0e10cSrcweir /** Set the prefix used to identify arguments in
148cdf0e10cSrcweir 	the command line.
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 	@precond	prefix is not empty
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 	@throws std::invalid_argument exception if
153cdf0e10cSrcweir 	the prefix is empty
154cdf0e10cSrcweir */
set_arg_prefix(const std::string & Prefix)155cdf0e10cSrcweir void CommandLine::set_arg_prefix(const std::string& Prefix)
156cdf0e10cSrcweir {
157cdf0e10cSrcweir 	OSL_PRECOND(Prefix.length(), "Empty argument prefix!");
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	if (0 == Prefix.length())
160cdf0e10cSrcweir 		throw std::invalid_argument("Empty argument prefix not allowed");
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	m_argprefix = Prefix;
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 
166cdf0e10cSrcweir /** Returns whether a given argument is an argument name
167cdf0e10cSrcweir */
is_arg_name(const std::string & Argument) const168cdf0e10cSrcweir bool CommandLine::is_arg_name(const std::string& Argument) const
169cdf0e10cSrcweir {
170cdf0e10cSrcweir 	return (0 == Argument.compare(0, m_argprefix.length(), m_argprefix));
171cdf0e10cSrcweir }
172