args_option.hh

Go to the documentation of this file.
00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2005, 2006, 2007, 2008, 2009 Ciaran McCreesh
00005  * Copyright (c) 2006 Stephen Bennett
00006  *
00007  * This file is part of the Paludis package manager. Paludis is free software;
00008  * you can redistribute it and/or modify it under the terms of the GNU General
00009  * Public License version 2, as published by the Free Software Foundation.
00010  *
00011  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00014  * details.
00015  *
00016  * You should have received a copy of the GNU General Public License along with
00017  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00018  * Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 #ifndef PALUDIS_GUARD_ARGS_ARGS_OPTION_HH
00022 #define PALUDIS_GUARD_ARGS_ARGS_OPTION_HH 1
00023 
00024 #include <paludis/args/args_visitor.hh>
00025 #include <paludis/util/private_implementation_pattern.hh>
00026 #include <paludis/util/wrapped_forward_iterator-fwd.hh>
00027 #include <paludis/util/type_list.hh>
00028 #include <paludis/util/named_value.hh>
00029 
00030 /** \file
00031  * Declarations for ArgsOption.
00032  *
00033  * \ingroup g_args
00034  *
00035  * \section Examples
00036  *
00037  * - None at this time.
00038  */
00039 
00040 namespace paludis
00041 {
00042     namespace n
00043     {
00044         struct description;
00045         struct long_name;
00046         struct short_name;
00047     }
00048 
00049     namespace args
00050     {
00051         class ArgsGroup;
00052 
00053         /**
00054          * Base class for a command line option.
00055          *
00056          * \ingroup g_args
00057          */
00058         class PALUDIS_VISIBLE ArgsOption :
00059             public virtual DeclareAbstractAcceptMethods<ArgsOption, MakeTypeList<
00060                     StringArg, AliasArg, SwitchArg, IntegerArg, EnumArg, StringSetArg, StringSequenceArg>::Type>
00061         {
00062             friend class ArgsHandler;
00063 
00064             private:
00065                 ArgsGroup * const _group;
00066 
00067                 const std::string _long_name;
00068                 const char _short_name;
00069                 const std::string _description;
00070 
00071                 bool _specified;
00072 
00073                 ArgsOption(const ArgsOption &);
00074                 void operator= (const ArgsOption &);
00075 
00076             protected:
00077                 /**
00078                  * Constructor.
00079                  */
00080                 ArgsOption(ArgsGroup * const, const std::string & long_name,
00081                         const char short_name, const std::string & description);
00082 
00083                 /**
00084                  * Destructor.
00085                  */
00086                 virtual ~ArgsOption();
00087 
00088             public:
00089                 /**
00090                  * Remove this option.
00091                  */
00092                 void remove();
00093 
00094                 /**
00095                  * Fetch our long name.
00096                  */
00097                 const std::string & long_name() const
00098                 {
00099                     return _long_name;
00100                 }
00101 
00102                 /**
00103                  * Fetch our short name (may be 0).
00104                  */
00105                 char short_name() const
00106                 {
00107                     return _short_name;
00108                 }
00109 
00110                 /**
00111                  * Fetch our description.
00112                  */
00113                 const std::string & description() const
00114                 {
00115                     return _description;
00116                 }
00117 
00118                 /**
00119                  * Fetch whether or not we were specified on the
00120                  * command line.
00121                  */
00122                 virtual bool specified() const
00123                 {
00124                     return _specified;
00125                 }
00126 
00127                 /**
00128                  * Set the value returned by specified().
00129                  */
00130                 virtual void set_specified(const bool value)
00131                 {
00132                     _specified = value;
00133                 }
00134 
00135                 /**
00136                  * Fetch our group.
00137                  */
00138                 ArgsGroup * group()
00139                 {
00140                     return _group;
00141                 }
00142 
00143                 /**
00144                  * Can we be negated?
00145                  *
00146                  * Needs to match up with ArgsVisitor logic.
00147                  */
00148                 virtual bool can_be_negated() const = 0;
00149 
00150                 /**
00151                  * Ourself as a forwardable string.
00152                  *
00153                  * For example, '--foo bar' or '--foo bar --foo baz' or '--foo', or
00154                  * if not specified, the empty string.
00155                  *
00156                  * \since 0.40
00157                  */
00158                 virtual const std::string forwardable_string() const PALUDIS_ATTRIBUTE((warn_unused_result)) = 0;
00159         };
00160 
00161         /**
00162          * A SwitchArg is an option that can either be specified or not
00163          * specified, and that takes no value (for example, --help).
00164          *
00165          * \ingroup g_args
00166          */
00167         class PALUDIS_VISIBLE SwitchArg :
00168             public ArgsOption,
00169             public ImplementAcceptMethods<ArgsOption, SwitchArg>
00170         {
00171             private:
00172                 bool _can_be_negated;
00173 
00174             public:
00175                 /**
00176                  * Constructor.
00177                  *
00178                  * \since 0.26
00179                  */
00180                 SwitchArg(ArgsGroup * const group, const std::string & long_name, char short_name,
00181                         const std::string & description, const bool can_be_negated);
00182 
00183                 ~SwitchArg();
00184 
00185                 virtual bool can_be_negated() const;
00186 
00187                 virtual const std::string forwardable_string() const PALUDIS_ATTRIBUTE((warn_unused_result));
00188         };
00189 
00190         /**
00191          * An option that takes a string argument.
00192          *
00193          * \ingroup g_args
00194          */
00195         class PALUDIS_VISIBLE StringArg :
00196             public ArgsOption,
00197             public ImplementAcceptMethods<ArgsOption, StringArg>
00198         {
00199             private:
00200                 std::string _argument;
00201                 void (* _validator) (const std::string &);
00202 
00203             public:
00204                 /**
00205                 * Constructor
00206                 */
00207                 StringArg(ArgsGroup * const, const std::string & long_name,
00208                        const char short_name, const std::string & description);
00209 
00210                 /**
00211                  * Constructor with validator.
00212                  */
00213                 StringArg(ArgsGroup * const, const std::string & long_name,
00214                        const char short_name, const std::string & description,
00215                        void (* validator) (const std::string &));
00216 
00217                 /**
00218                  * Fetch the argument that was given to this option.
00219                  */
00220                 const std::string & argument() const { return _argument; }
00221 
00222                 /**
00223                  * Set the argument returned by argument().
00224                  */
00225                 void set_argument(const std::string & arg);
00226 
00227                 virtual bool can_be_negated() const;
00228 
00229                 virtual const std::string forwardable_string() const PALUDIS_ATTRIBUTE((warn_unused_result));
00230         };
00231 
00232         /**
00233          * An option that takes a set of strings.
00234          *
00235          * \ingroup g_args
00236          * \nosubgrouping
00237          */
00238         class PALUDIS_VISIBLE StringSetArg :
00239             public ArgsOption,
00240             public ImplementAcceptMethods<ArgsOption, StringSetArg>,
00241             private PrivateImplementationPattern<StringSetArg>
00242         {
00243             private:
00244                 void (* _validator) (const std::string &);
00245 
00246             public:
00247                 /**
00248                  * Helper class for passing available options and associated descriptions
00249                  * to the StringSetArg constructor.
00250                  *
00251                  * \ingroup grplibpaludisargs
00252                  */
00253                 class PALUDIS_VISIBLE StringSetArgOptions :
00254                     private PrivateImplementationPattern<StringSetArgOptions>
00255                 {
00256                     friend class StringSetArg;
00257 
00258                     public:
00259                         /**
00260                          * Constructor
00261                          */
00262                         StringSetArgOptions(const std::string &, const std::string &);
00263 
00264                         /**
00265                          * Blank constructor
00266                          */
00267                         explicit StringSetArgOptions();
00268 
00269                         /**
00270                          * Copy constructor
00271                          */
00272                         StringSetArgOptions(const StringSetArgOptions &);
00273 
00274                         /**
00275                          * Destructor.
00276                          */
00277                         ~StringSetArgOptions();
00278 
00279                         /**
00280                          * Adds another (option, description) pair.
00281                          */
00282                         StringSetArgOptions & operator() (const std::string &, const std::string &);
00283                 };
00284 
00285                 ///\name Basic operations
00286                 ///\{
00287 
00288                 StringSetArg(ArgsGroup * const, const std::string & long_name,
00289                         const char short_name, const std::string & description,
00290                         const StringSetArgOptions & options = StringSetArgOptions());
00291 
00292                 StringSetArg(ArgsGroup * const, const std::string & long_name,
00293                         const char short_name, const std::string & description,
00294                         const StringSetArgOptions & options,
00295                         void (* validator) (const std::string &));
00296 
00297                 ~StringSetArg();
00298 
00299                 ///\}
00300 
00301                 ///\name Iterate over our args.
00302                 ///\{
00303 
00304                 struct ConstIteratorTag;
00305                 typedef WrappedForwardIterator<ConstIteratorTag, const std::string> ConstIterator;
00306 
00307                 ConstIterator begin_args() const;
00308 
00309                 ConstIterator end_args() const;
00310 
00311                 ///\}
00312 
00313                 /**
00314                  * Add an argument to the set.
00315                  */
00316                 void add_argument(const std::string & arg);
00317 
00318                 ///\name Iterate over our allowed arguments and associated descriptions
00319                 ///\{
00320 
00321                 struct AllowedArgConstIteratorTag;
00322                 typedef WrappedForwardIterator<AllowedArgConstIteratorTag, 
00323                         const std::pair<std::string, std::string> > AllowedArgConstIterator;
00324 
00325                 AllowedArgConstIterator begin_allowed_args() const;
00326 
00327                 AllowedArgConstIterator end_allowed_args() const;
00328 
00329                 ///\}
00330 
00331                 virtual bool can_be_negated() const;
00332 
00333                 virtual const std::string forwardable_string() const PALUDIS_ATTRIBUTE((warn_unused_result));
00334         };
00335 
00336         /**
00337          * An option that takes a set of strings.
00338          *
00339          * \since 0.32
00340          * \ingroup g_args
00341          * \nosubgrouping
00342          */
00343         class PALUDIS_VISIBLE StringSequenceArg :
00344             public ArgsOption,
00345             public ImplementAcceptMethods<ArgsOption, StringSequenceArg>,
00346             private PrivateImplementationPattern<StringSequenceArg>
00347         {
00348             public:
00349                 ///\name Basic operations
00350                 ///\{
00351 
00352                 StringSequenceArg(ArgsGroup * const, const std::string & long_name,
00353                         const char short_name, const std::string & description);
00354 
00355                 ~StringSequenceArg();
00356 
00357                 ///\}
00358 
00359                 ///\name Iterate over our args.
00360                 ///\{
00361 
00362                 struct ConstIteratorTag;
00363                 typedef WrappedForwardIterator<ConstIteratorTag, const std::string> ConstIterator;
00364 
00365                 ConstIterator begin_args() const;
00366 
00367                 ConstIterator end_args() const;
00368 
00369                 ///\}
00370 
00371                 /**
00372                  * Add an argument to the set.
00373                  */
00374                 void add_argument(const std::string & arg);
00375 
00376                 virtual bool can_be_negated() const;
00377 
00378                 virtual const std::string forwardable_string() const PALUDIS_ATTRIBUTE((warn_unused_result));
00379         };
00380 
00381 
00382         /**
00383          * An AliasArg is an alias for another argument.
00384          *
00385          * \ingroup g_args
00386          */
00387         class PALUDIS_VISIBLE AliasArg :
00388             public ArgsOption,
00389             public ImplementAcceptMethods<ArgsOption, AliasArg>
00390         {
00391             private:
00392                 ArgsOption * const _other;
00393                 bool _hidden;
00394 
00395             public:
00396                 /**
00397                  * Constructor.
00398                  */
00399                 AliasArg(ArgsOption * const other, const std::string & new_long_name, bool is_hidden = false);
00400 
00401                 virtual bool specified() const
00402                 {
00403                     return _other->specified();
00404                 }
00405 
00406                 virtual void set_specified(const bool value)
00407                 {
00408                     _other->set_specified(value);
00409                 }
00410 
00411                 virtual bool hidden() const
00412                 {
00413                     return _hidden;
00414                 }
00415 
00416                 virtual void set_hidden(const bool value)
00417                 {
00418                     _hidden = value;
00419                 }
00420 
00421                 /**
00422                  * Fetch our associated option.
00423                  */
00424                 ArgsOption * other() const
00425                 {
00426                     return _other;
00427                 }
00428 
00429                 virtual bool can_be_negated() const;
00430 
00431                 virtual const std::string forwardable_string() const PALUDIS_ATTRIBUTE((warn_unused_result));
00432         };
00433 
00434         /**
00435          * An option that takes an integer argument.
00436          *
00437          * \ingroup grplibpaludisargs
00438          */
00439         class PALUDIS_VISIBLE IntegerArg :
00440             public ArgsOption,
00441             public ImplementAcceptMethods<ArgsOption, IntegerArg>
00442         {
00443             private:
00444                 int _argument;
00445 
00446             public:
00447                 /**
00448                  * Constructor
00449                  */
00450                 IntegerArg(ArgsGroup * const, const std::string & long_name,
00451                         const char short_name, const std::string & description);
00452                 /**
00453                  * Fetch the argument that was given to this option.
00454                  */
00455                 int argument() const { return _argument; }
00456 
00457                 /**
00458                  * Set the argument returned by argument().
00459                  */
00460                 void set_argument(const int arg) { _argument = arg; }
00461 
00462                 virtual bool can_be_negated() const;
00463 
00464                 virtual const std::string forwardable_string() const PALUDIS_ATTRIBUTE((warn_unused_result));
00465         };
00466 
00467         /**
00468          * An allowed argument for an EnumArg.
00469          *
00470          * \ingroup g_args
00471          * \since 0.40
00472          */
00473         struct AllowedEnumArg
00474         {
00475             NamedValue<n::description, std::string> description;
00476             NamedValue<n::long_name, std::string> long_name;
00477 
00478             /// Might be '\0', for none.
00479             NamedValue<n::short_name, char> short_name;
00480         };
00481 
00482         /**
00483          * An option that takes one of a predefined set of string arguments.
00484          *
00485          * \ingroup g_args
00486          * \nosubgrouping
00487          */
00488         class PALUDIS_VISIBLE EnumArg :
00489             public ArgsOption,
00490             public ImplementAcceptMethods<ArgsOption, EnumArg>,
00491             private PrivateImplementationPattern<EnumArg>
00492         {
00493             private:
00494                 std::string _argument;
00495                 std::string _default_arg;
00496 
00497             public:
00498                 /**
00499                  * Helper class for passing available options and associated descriptions
00500                  * to the EnumArg constructor.
00501                  *
00502                  * \ingroup grplibpaludisargs
00503                  */
00504                 class PALUDIS_VISIBLE EnumArgOptions :
00505                     private PrivateImplementationPattern<EnumArgOptions>
00506                 {
00507                     friend class EnumArg;
00508 
00509                     public:
00510                         /**
00511                          * Constructor
00512                          */
00513                         EnumArgOptions(const std::string &, const std::string &);
00514 
00515                         /**
00516                          * Constructor, with short arg.
00517                          *
00518                          * \since 0.40
00519                          */
00520                         EnumArgOptions(const std::string &, const char, const std::string &);
00521 
00522                         /**
00523                          * Destructor.
00524                          */
00525                         ~EnumArgOptions();
00526 
00527                         /**
00528                          * Adds another (option, description).
00529                          */
00530                         EnumArgOptions & operator() (const std::string &, const std::string &);
00531 
00532                         /**
00533                          * Adds another (option, short-option, description).
00534                          *
00535                          * \since 0.40
00536                          */
00537                         EnumArgOptions & operator() (const std::string &, const char, const std::string &);
00538                 };
00539 
00540                 /**
00541                  * Constructor.
00542                  */
00543                 EnumArg(ArgsGroup * const group, const std::string & long_name,
00544                         const char short_name, const std::string & description,
00545                         const EnumArgOptions & opts, const std::string & default_arg);
00546 
00547                 ~EnumArg();
00548 
00549                 /**
00550                  * Fetch the argument that was given to this option.
00551                  */
00552                 const std::string & argument() const
00553                 {
00554                     return _argument;
00555                 }
00556 
00557                 /**
00558                  * Set the argument returned by argument(), having verified that
00559                  * it is one of the arguments allowed for this option.
00560                  */
00561                 void set_argument(const std::string & arg);
00562 
00563                 /**
00564                  * Change the default option (should be called before
00565                  * set_argument()).
00566                  */
00567                 void set_default_arg(const std::string & arg);
00568 
00569                 /**
00570                  * Fetch the default option, as specified to the
00571                  * constructor or set_default_arg().
00572                  */
00573                 const std::string & default_arg() const
00574                 {
00575                     return _default_arg;
00576                 }
00577 
00578                 ///\name Iterate over our allowed arguments and associated descriptions
00579                 ///\{
00580 
00581                 struct AllowedArgConstIteratorTag;
00582                 typedef WrappedForwardIterator<AllowedArgConstIteratorTag,
00583                         const AllowedEnumArg> AllowedArgConstIterator;
00584 
00585                 AllowedArgConstIterator begin_allowed_args() const;
00586 
00587                 AllowedArgConstIterator end_allowed_args() const;
00588 
00589                 ///\}
00590 
00591                 virtual bool can_be_negated() const;
00592 
00593                 virtual const std::string forwardable_string() const PALUDIS_ATTRIBUTE((warn_unused_result));
00594         };
00595     }
00596 }
00597 
00598 #endif

Generated on Mon Sep 21 10:36:08 2009 for paludis by  doxygen 1.5.4