aboutsummaryrefslogtreecommitdiff
path: root/paludis/args/args_handler.hh
blob: f1e7088679e3abb56c75f6d5d8e3132e95008c8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef PALUDIS_GUARD_ARGS_ARGS_HANDLER_HH
#define PALUDIS_GUARD_ARGS_ARGS_HANDLER_HH 1

#include <paludis/args/args_section.hh>
#include <paludis/args/args_group.hh>
#include <paludis/util/pimp.hh>
#include <paludis/util/options.hh>
#include <paludis/util/sequence.hh>
#include <memory>
#include <iosfwd>
#include <string>

/** \file
 * Declarations for ArgsHandler.
 *
 * \ingroup g_args
 *
 * \section Examples
 *
 * - None at this time.
 */

namespace paludis
{
    namespace args
    {
        class ArgsOption;

#include <paludis/args/args_handler-se.hh>

        typedef Options<ArgsHandlerOption> ArgsHandlerOptions;

        /**
         * Handles command line arguments.
         *
         * \ingroup g_args
         * \nosubgrouping
         */
        class PALUDIS_VISIBLE ArgsHandler
        {
            friend class ArgsSection;
            friend std::ostream & operator<< (std::ostream &, const ArgsHandler &);

            private:
                Pimp<ArgsHandler> _imp;

            protected:
                /**
                 * Add a new usage line.
                 */
                void add_usage_line(const std::string & l);

                /**
                 * Add a new environment line.
                 */
                void add_environment_variable(const std::string & e, const std::string & desc);

                /**
                 * Add a new example.
                 */
                void add_example(const std::string & e, const std::string & desc);

                /**
                 * Add a new note.
                 */
                void add_note(const std::string &);

                /**
                 * Add a new description.
                 */
                void add_description_line(const std::string & l);

                /**
                 * Add a 'see also' item.
                 *
                 * \since 0.48.2
                 */
                void add_see_also(const std::string &, int section);

                /**
                 * Add an new ArgsSection (called by the ArgsSection constructor).
                 */
                void add(ArgsSection * const);

                /**
                 * Dump, for --help output (called by operator<<).
                 */
                void dump_to_stream(std::ostream & s) const;

                /**
                 * Called after run(), for convenience. Does nothing.
                 */
                virtual void post_run();

            public:
                ///\name Basic operations
                ///\{

                ArgsHandler();

                virtual ~ArgsHandler();

                ArgsHandler(const ArgsHandler &) = delete;

                ArgsHandler & operator= (const ArgsHandler &) = delete;

                ///\}

                ///\name Iterate over our parameters (non - and -- switches and their values)
                ///\{

                struct ParametersConstIteratorTag;
                typedef WrappedForwardIterator<ParametersConstIteratorTag, const std::string> ParametersConstIterator;

                ParametersConstIterator begin_parameters() const;
                ParametersConstIterator end_parameters() const;

                IteratorRange<ParameterConstIterator> parameters() const {
                    return make_iterator_range(begin_parameters(), end_parameters());
                }

                bool empty() const;

                /**
                 * If aho_separate_after_dashes, everything after a -- goes
                 * here.
                 *
                 * \since 0.47
                 */
                const std::shared_ptr<const Sequence<std::string> > separate_after_dashes_args() const
                    PALUDIS_ATTRIBUTE((warn_unused_result));

                ///\}

                /**
                 * Add an ArgsOption instance.
                 */
                void add_option(ArgsOption * const, const std::string & long_name,
                        const char short_name = '\0');

                /**
                 * Remove an ArgsOption instance.
                 */
                void remove_option(const std::string & long_name, const char short_name = '\0');

                ///\name About our application (for documentation)
                ///\{

                /**
                 * What is our application name?
                 */
                virtual std::string app_name() const = 0;

                /**
                 * What is our application's Unix manual section?
                 */
                virtual std::string man_section() const
                {
                    return "1";
                }

                /**
                 * One line synopsis of what our application is.
                 */
                virtual std::string app_synopsis() const = 0;

                /**
                 * Long description of what our application is.
                 */
                virtual std::string app_description() const = 0;

                ///\}

                ///\name Iterate over our usage lines (for documentation)
                ///\{

                struct UsageLineConstIteratorTag;
                typedef WrappedForwardIterator<UsageLineConstIteratorTag, const std::string> UsageLineConstIterator;

                UsageLineConstIterator begin_usage_lines() const;
                UsageLineConstIterator end_usage_lines() const;

                IteratorRange<UsageLineConstIterator> usage_lines() const {
                    return make_iterator_range(begin_usage_lines(), end_usage_lines());
                }

                ///\}

                ///\name Iterate over our environment lines (for documentation)
                ///\{

                struct EnvironmentLineConstIteratorTag;
                typedef WrappedForwardIterator<EnvironmentLineConstIteratorTag,
                        const std::pair<std::string, std::string> > EnvironmentLineConstIterator;

                EnvironmentLineConstIterator begin_environment_lines() const;
                EnvironmentLineConstIterator end_environment_lines() const;

                IteratorRange<EnvironmentLineConstIterator> environment_lines() const {
                    return make_iterator_range(begin_environment_lines(), end_environment_lines());
                }

                ///\}

                ///\name Iterate over our examples (for documentation)
                ///\{

                struct ExamplesConstIteratorTag;
                typedef WrappedForwardIterator<ExamplesConstIteratorTag,
                        const std::pair<std::string, std::string> > ExamplesConstIterator;

                ExamplesConstIterator begin_examples() const;
                ExamplesConstIterator end_examples() const;

                IteratorRange<ExamplesConstIterator> examples() const {
                    return make_iterator_range(begin_examples(), end_examples());
                }

                ///\}

                ///\name Iterate over our sections
                ///\{

                struct ArgsSectionsConstIteratorTag;
                typedef WrappedForwardIterator<ArgsSectionsConstIteratorTag, const ArgsSection> ArgsSectionsConstIterator;

                ArgsSectionsConstIterator begin_args_sections() const;
                ArgsSectionsConstIterator end_args_sections() const;

                IteratorRange<ArgsSectionsConstIterator> args_sections() const {
                    return make_iterator_range(begin_args_sections(), end_args_sections());
                }

                /**
                 * The 'Options' section.
                 *
                 * Created if it does not exist.
                 */
                ArgsSection * main_options_section() PALUDIS_ATTRIBUTE((warn_unused_result));

                ///\}

                ///\name Iterate over our notes
                ///\{

                struct NotesIteratorTag;
                typedef WrappedForwardIterator<NotesIteratorTag, const std::string > NotesIterator;

                NotesIterator begin_notes() const;
                NotesIterator end_notes() const;

                IteratorRange<NotesIterator> notes() const {
                    return make_iterator_range(begin_notes(), end_notes());
                }

                ///\}

                ///\name Iterate over our extra description lines (for documentation)
                ///\{

                struct DescriptionLineConstIteratorTag;
                typedef WrappedForwardIterator<DescriptionLineConstIteratorTag, const std::string> DescriptionLineConstIterator;

                DescriptionLineConstIterator begin_description_lines() const;
                DescriptionLineConstIterator end_description_lines() const;

                IteratorRange<DescriptionLineConstIterator> description_lines() const {
                    return make_iterator_range(begin_description_lines(), end_description_lines());
                }

                ///\}

                ///\name Iterate over our 'see also' lines
                ///\since 0.48.2
                ///\{

                struct SeeAlsoConstIteratorTag;
                typedef WrappedForwardIterator<SeeAlsoConstIteratorTag, const std::pair<std::string, int> > SeeAlsoConstIterator;

                SeeAlsoConstIterator begin_see_alsos() const;
                SeeAlsoConstIterator end_see_alsos() const;

                IteratorRange<SeeAlsoConstIterator> see_alsos() const {
                    return make_iterator_range(begin_see_alsos(), end_see_alsos());
                }

                ///\}

                ///\name For use by ArgsVisitor
                ///\{

                struct ArgsIteratorTag;
                typedef WrappedForwardIterator<ArgsIteratorTag, std::string> ArgsIterator;

                ///\}

                /**
                 * Parse command line arguments. The third argument is used to
                 * set PALUDIS_CLIENT.  The fourth argument is the name of an
                 * environment variable holding arguments which are prepended
                 * to the command line arguments. The fifth argument is used as
                 * a prefix to export our command line via the environment.
                 */
                void run(
                        const int argc,
                        const char * const * const argv,
                        const std::string & client,
                        const std::string & env_var,
                        const std::string & env_prefix,
                        const ArgsHandlerOptions & options = ArgsHandlerOptions());

                /**
                 * Parse command line arguments. The third argument is used to
                 * set PALUDIS_CLIENT.  The fourth argument is the name of an
                 * environment variable holding arguments which are prepended
                 * to the command line arguments. The fifth argument is used as
                 * a prefix to export our command line via the environment.
                 */
                void run(
                        const std::shared_ptr<const Sequence<std::string> > &,
                        const std::string & client,
                        const std::string & env_var,
                        const std::string & env_prefix,
                        const ArgsHandlerOptions & options = ArgsHandlerOptions());
        };

        /**
         * Output an ArgsHandler to an ostream, for --help output.
         *
         * \ingroup g_args
         */
        std::ostream & operator<< (std::ostream &, const ArgsHandler &) PALUDIS_VISIBLE;
    }

    extern template class PALUDIS_VISIBLE WrappedForwardIterator<args::ArgsHandler::ParametersConstIteratorTag, const std::string>;
    extern template class PALUDIS_VISIBLE WrappedForwardIterator<args::ArgsHandler::UsageLineConstIteratorTag, const std::string>;
    extern template class PALUDIS_VISIBLE WrappedForwardIterator<args::ArgsHandler::EnvironmentLineConstIteratorTag,
            const std::pair<std::string, std::string> >;
    extern template class PALUDIS_VISIBLE WrappedForwardIterator<args::ArgsHandler::ExamplesConstIteratorTag,
            const std::pair<std::string, std::string> >;
    extern template class PALUDIS_VISIBLE WrappedForwardIterator<args::ArgsHandler::ArgsSectionsConstIteratorTag, const args::ArgsSection>;
    extern template class PALUDIS_VISIBLE WrappedForwardIterator<args::ArgsHandler::DescriptionLineConstIteratorTag, const std::string>;
    extern template class PALUDIS_VISIBLE WrappedForwardIterator<args::ArgsHandler::NotesIteratorTag, const std::string>;
    extern template class PALUDIS_VISIBLE WrappedForwardIterator<args::ArgsHandler::ArgsIteratorTag, std::string>;
    extern template class PALUDIS_VISIBLE WrappedForwardIterator<args::ArgsHandler::SeeAlsoConstIteratorTag, const std::pair<std::string, int> >;
}

#endif