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