SGetOpts.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef APP_SGETOPTS_H
00014 #define APP_SGETOPTS_H
00015 
00016 #include <support/ITextStream.h>
00017 
00018 #include <app/BCommand.h>
00019 
00020 #if _SUPPORTS_NAMESPACE
00021 namespace palmos {
00022 namespace app {
00023 using namespace palmos::support;
00024 #endif
00025 
00026 enum
00027 {
00028     B_MAIN_ARGUMENT = 0,    // Option is really a main command argument
00029     B_NO_ARGUMENT,          // This option does not have an argument
00030     B_REQUIRED_ARGUMENT     // This option requires an argument
00031 };
00032 
00033 struct SLongOption
00034 {
00035     size_t                      size;   // bytes in this structure
00036     const char*                 name;
00037     int32_t                     type;   // one of B_*_ARGUMENT
00038     int32_t                     option; // single-character or int identifer
00039     const char*                 help;   // optional help text for this option
00040 
00041     inline SLongOption*         Next() { return (SLongOption*)(((size_t)this)+size); }
00042     inline const SLongOption*   Next() const { return (const SLongOption*)(((size_t)this)+size); }
00043 };
00044 
00045 class SGetOpts
00046 {
00047 public:
00048                         SGetOpts(const SLongOption* options);
00049                         ~SGetOpts();
00050 
00051     void                TheyNeedHelp();
00052     bool                IsHelpNeeded() const;
00053     void                PrintShortHelp(const sptr<ITextOutput>& out, const ICommand::ArgList& args);
00054     void                PrintHelp(const sptr<ITextOutput>& out, const ICommand::ArgList& args, const char* longHelp);
00055 
00056     // Returns > 0 for each valid option, 0 for each argument, -1 when done.
00057     // XXX Shouldn't pass in cmd here -- it is only used to get the text output
00058     // stream for printing errors.
00059     int32_t             Next(const sptr<BCommand>& cmd, const ICommand::ArgList& args);
00060 
00061     // Move to the next command argument.  Returns the new index, or an error
00062     // if there are no more arguments.
00063     ssize_t             NextArgument(const ICommand::ArgList& args);
00064 
00065     int32_t             ArgumentIndex() const;
00066     SValue              ArgumentValue() const;
00067 
00068     int32_t             OptionIndex() const;
00069 
00070 private:
00071                         SGetOpts(const SGetOpts&);
00072     SGetOpts&           operator=(const SGetOpts&);
00073 
00074     const SLongOption*  m_options;
00075     int32_t             m_curIndex;
00076     int32_t             m_optIndex;
00077     int32_t             m_endOfOpts;
00078     int32_t             m_optCode;
00079     SValue              m_argument;
00080     SValue              m_shortOpts;
00081     int32_t             m_shortPos;
00082     bool                m_helpNeeded;
00083 };
00084 
00085 
00086 #if _SUPPORTS_NAMESPACE
00087 } } // namespace palmos::app
00088 #endif
00089 
00090 #endif // APP_BCOMMAND_H