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