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
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_shell.hxx"
26 #include <stdexcept>
27 #include <osl/diagnose.h>
28 #include "cmdline.hxx"
29
30 //---------------------------------
31 /** Simple command line abstraction
32 */
33
34 //################################
35 // Creation
36 //################################
37
38
CommandLine(size_t argc,char * argv[],const std::string & ArgPrefix)39 CommandLine::CommandLine(size_t argc, char* argv[], const std::string& ArgPrefix) :
40 m_argc(argc),
41 m_argv(argv),
42 m_argprefix(ArgPrefix)
43 {
44 }
45
46
47 //################################
48 // Query
49 //################################
50
51
52 /** Return the argument count
53 */
get_arg_count() const54 size_t CommandLine::get_arg_count() const
55 {
56 return m_argc;
57 }
58
59 /** Return an argument by index
60 This method doesn't skip argument
61 names if any, so if the second
62 argument is an argument name the
63 function nevertheless returns it.
64
65 @precond 0 <= Index < GetArgumentCount
66
67 @throws std::out_of_range exception
68 if the given index is to high
69 */
get_arg(size_t Index) const70 std::string CommandLine::get_arg(size_t Index) const
71 {
72 OSL_PRECOND(Index < m_argc, "Index out of range");
73
74 if (Index > (m_argc - 1))
75 throw std::out_of_range("Invalid index");
76
77 return m_argv[Index];
78 }
79
80
81 /** Returns all argument name found in the
82 command line. An argument will be identified
83 by a specified prefix. The standard prefix
84 is '-'.
85 If the are no argument names the returned
86 container is empty.
87 */
get_arg_names() const88 StringListPtr_t CommandLine::get_arg_names() const
89 {
90 StringListPtr_t arg_cont(new StringList_t());
91
92 for (size_t i = 0; i < m_argc; i++)
93 {
94 std::string argn = m_argv[i];
95
96 if (is_arg_name(argn))
97 arg_cont->push_back(argn);
98 }
99
100 return arg_cont;
101 }
102
103 /** Returns an argument by name. If there are
104 duplicate argument names in the command line,
105 the first one wins.
106 Argument name and the argument value must be separated
107 by spaces. If the argument value starts with an
108 argument prefix use quotes else the return value is
109 an empty string because the value will be interpreted
110 as the next argument name.
111 If an argument value contains spaces use quotes.
112
113 @precond GetArgumentNames() -> has element ArgumentName
114
115 @throws std::invalid_argument exception
116 if the specified argument could not be
117 found
118 */
get_arg(const std::string & ArgumentName) const119 std::string CommandLine::get_arg(const std::string& ArgumentName) const
120 {
121 std::string arg_value;
122 size_t i;
123 for ( i = 0; i < m_argc; i++)
124 {
125 std::string arg = m_argv[i];
126
127 if (ArgumentName == arg && ((i+1) < m_argc) && !is_arg_name(m_argv[i+1]))
128 {
129 arg_value = m_argv[i+1];
130 break;
131 }
132 }
133
134 if (i == m_argc)
135 throw std::invalid_argument("Invalid argument name");
136
137 return arg_value;
138 }
139
140
141 //################################
142 // Command
143 //################################
144
145
146 /** Set the prefix used to identify arguments in
147 the command line.
148
149 @precond prefix is not empty
150
151 @throws std::invalid_argument exception if
152 the prefix is empty
153 */
set_arg_prefix(const std::string & Prefix)154 void CommandLine::set_arg_prefix(const std::string& Prefix)
155 {
156 OSL_PRECOND(Prefix.length(), "Empty argument prefix!");
157
158 if (0 == Prefix.length())
159 throw std::invalid_argument("Empty argument prefix not allowed");
160
161 m_argprefix = Prefix;
162 }
163
164
165 /** Returns whether a given argument is an argument name
166 */
is_arg_name(const std::string & Argument) const167 bool CommandLine::is_arg_name(const std::string& Argument) const
168 {
169 return (0 == Argument.compare(0, m_argprefix.length(), m_argprefix));
170 }
171
172 /* vim: set noet sw=4 ts=4: */
173