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