xref: /aoo42x/main/autodoc/inc/cosv/commandline.hxx (revision 8a106958)
1ac3d0c65SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ac3d0c65SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ac3d0c65SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ac3d0c65SAndrew Rist  * distributed with this work for additional information
6ac3d0c65SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ac3d0c65SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ac3d0c65SAndrew Rist  * "License"); you may not use this file except in compliance
9ac3d0c65SAndrew Rist  * with the License.  You may obtain a copy of the License at
10ac3d0c65SAndrew Rist  *
11ac3d0c65SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ac3d0c65SAndrew Rist  *
13ac3d0c65SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ac3d0c65SAndrew Rist  * software distributed under the License is distributed on an
15ac3d0c65SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ac3d0c65SAndrew Rist  * KIND, either express or implied.  See the License for the
17ac3d0c65SAndrew Rist  * specific language governing permissions and limitations
18ac3d0c65SAndrew Rist  * under the License.
19ac3d0c65SAndrew Rist  *
20ac3d0c65SAndrew Rist  *************************************************************/
21ac3d0c65SAndrew Rist 
22ac3d0c65SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef CSV_COMMANDLINE_HXX
25cdf0e10cSrcweir #define CSV_COMMANDLINE_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <cosv/string.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir 
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace csv
33cdf0e10cSrcweir {
34cdf0e10cSrcweir 
35cdf0e10cSrcweir /** Does the standards in handling command line parameters.
36cdf0e10cSrcweir 
37cdf0e10cSrcweir     This class provides a default behaviour this way:
38cdf0e10cSrcweir 
39cdf0e10cSrcweir     *   Each option can have several forms
40cdf0e10cSrcweir 
41cdf0e10cSrcweir             like: "-f" and "--file"
42cdf0e10cSrcweir 
43cdf0e10cSrcweir         which are identified by having the same id.
44cdf0e10cSrcweir         The option id is used when calling ->do_HandleOption().
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     *   For each recognized option together with its parameters
47cdf0e10cSrcweir         ->do_HandleOption() is called.
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     *   For the first unrecognized argument ->do_HandleFreeArgument() is
50cdf0e10cSrcweir         called.
51cdf0e10cSrcweir         After the first unrecognized argument, ->do_HandleFreeArgument()
52cdf0e10cSrcweir         is called for all remaining arguments.
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     @howtoderive
55cdf0e10cSrcweir     -   Overwrite ->do_HandleOption() to act on all known options.
56cdf0e10cSrcweir         Overwrite ->do_HandleFreeArgument() to act on additional
57cdf0e10cSrcweir         arguments not connected to an option.
58cdf0e10cSrcweir */
59cdf0e10cSrcweir class CommandLine
60cdf0e10cSrcweir {
61cdf0e10cSrcweir   public:
62cdf0e10cSrcweir   // LIFECYCLE
~CommandLine()63cdf0e10cSrcweir     virtual             ~CommandLine() {}
64cdf0e10cSrcweir 
65cdf0e10cSrcweir   // OPERATIONS
66cdf0e10cSrcweir     bool                Interpret(
67cdf0e10cSrcweir                             int                 argc,
68cdf0e10cSrcweir                             char *              argv[] );
69cdf0e10cSrcweir   // INQUIRY
70cdf0e10cSrcweir     const StringVector &
71cdf0e10cSrcweir                         Arguments() const;
72cdf0e10cSrcweir     bool                IsOk() const;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir   protected:
75cdf0e10cSrcweir                         CommandLine();
76cdf0e10cSrcweir     void                Add_Option(
77cdf0e10cSrcweir                             intt                i_id,
78cdf0e10cSrcweir                             String              i_text );
79cdf0e10cSrcweir     void                Set_Error();
80cdf0e10cSrcweir 
81cdf0e10cSrcweir   private:
82cdf0e10cSrcweir     // public for use by struct commandline.cxx-anonymous::FindOptionByText;
83cdf0e10cSrcweir     struct OptionDescription
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         intt                nId;
86cdf0e10cSrcweir         String              sText;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir                             OptionDescription(
89cdf0e10cSrcweir                                 intt                i_id,
90cdf0e10cSrcweir                                 String              i_text );
91cdf0e10cSrcweir     }; private:
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     struct FindOptionByText;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     typedef std::vector<OptionDescription>              OptionList;
96cdf0e10cSrcweir     typedef std::vector<StringVector::const_iterator>   StringCIteratorList;
97cdf0e10cSrcweir     typedef std::vector<intt>                           OptionIdList;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     // Locals
100cdf0e10cSrcweir     void                Get_Arguments(
101cdf0e10cSrcweir                             int                 argc,
102cdf0e10cSrcweir                             char *              argv[] );
103cdf0e10cSrcweir     intt                Find_Option(
104cdf0e10cSrcweir                             const String &      i_text ) const;
105cdf0e10cSrcweir     bool                Store_Argument(
106cdf0e10cSrcweir                             const String &      i_arg );
107cdf0e10cSrcweir     void                Find_OptionPoints();
108cdf0e10cSrcweir     void                Handle_FreeArguments(
109cdf0e10cSrcweir                             StringVector::const_iterator
110cdf0e10cSrcweir                                                 i_begin,
111cdf0e10cSrcweir                             StringVector::const_iterator
112cdf0e10cSrcweir                                                 i_end );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     // Helpers for options included via file
115cdf0e10cSrcweir     bool                Try2Include_Options(
116cdf0e10cSrcweir                             const String &      i_optionsFile );
117cdf0e10cSrcweir     bool                Include_Options(
118cdf0e10cSrcweir                             const String &      i_optionsFile );
119cdf0e10cSrcweir     bool                Load_Options(
120cdf0e10cSrcweir                             StreamStr &         o_text,
121cdf0e10cSrcweir                             const String &      i_optionsFile );
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     /** Handles an option found in the command line.
124cdf0e10cSrcweir         Needs to be overwritten.
125cdf0e10cSrcweir 
126cdf0e10cSrcweir         @return
127cdf0e10cSrcweir         The first argument within the range
128cdf0e10cSrcweir         i_next_argument .. i_comandLine_end that does not belong as a
129cdf0e10cSrcweir         parameter to the handled option.
130cdf0e10cSrcweir     */
131cdf0e10cSrcweir     virtual StringVector::const_iterator
132cdf0e10cSrcweir                         do_HandleOption(
133cdf0e10cSrcweir                             intt                i_id,
134cdf0e10cSrcweir                             StringVector::const_iterator
135cdf0e10cSrcweir                                                 i_paramsBegin,
136cdf0e10cSrcweir                             StringVector::const_iterator
137cdf0e10cSrcweir                                                 i_paramsEnd ) = 0;
138cdf0e10cSrcweir     /** Handles arguments on the command line that do not belong to
139cdf0e10cSrcweir         an option.
140cdf0e10cSrcweir     */
141cdf0e10cSrcweir     virtual void        do_HandleFreeArgument(
142cdf0e10cSrcweir                             const String &      i_argument ) = 0;
143cdf0e10cSrcweir     // DATA
144cdf0e10cSrcweir     OptionList          aOptions;
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     /// Used during and after ->GetArguments()
147cdf0e10cSrcweir     StringVector        aCommandLine;
148cdf0e10cSrcweir     StringCIteratorList aOptionPoints;
149cdf0e10cSrcweir     OptionIdList        aOptionIds;
150cdf0e10cSrcweir     bool                bIsOk;
151cdf0e10cSrcweir };
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 
154cdf0e10cSrcweir inline const StringVector &
Arguments() const155cdf0e10cSrcweir CommandLine::Arguments() const
156cdf0e10cSrcweir {
157cdf0e10cSrcweir     return aCommandLine;
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir inline bool
IsOk() const161cdf0e10cSrcweir CommandLine::IsOk() const
162cdf0e10cSrcweir {
163cdf0e10cSrcweir     return bIsOk;
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir inline void
Set_Error()167cdf0e10cSrcweir CommandLine::Set_Error()
168cdf0e10cSrcweir {
169cdf0e10cSrcweir     bIsOk = false;
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 
175cdf0e10cSrcweir }   // namespace csv
176cdf0e10cSrcweir #endif
177