Home | History | Annotate | Line # | Download | only in tools
      1 //
      2 // Automated Testing Framework (atf)
      3 //
      4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
      5 // All rights reserved.
      6 //
      7 // Redistribution and use in source and binary forms, with or without
      8 // modification, are permitted provided that the following conditions
      9 // are met:
     10 // 1. Redistributions of source code must retain the above copyright
     11 //    notice, this list of conditions and the following disclaimer.
     12 // 2. Redistributions in binary form must reproduce the above copyright
     13 //    notice, this list of conditions and the following disclaimer in the
     14 //    documentation and/or other materials provided with the distribution.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 //
     29 
     30 #if !defined(TOOLS_APPLICATION_HPP)
     31 #define TOOLS_APPLICATION_HPP
     32 
     33 #include <ostream>
     34 #include <set>
     35 #include <stdexcept>
     36 #include <string>
     37 
     38 namespace tools {
     39 namespace application {
     40 
     41 // ------------------------------------------------------------------------
     42 // The "usage_error" class.
     43 // ------------------------------------------------------------------------
     44 
     45 class usage_error : public std::runtime_error {
     46     char m_text[4096];
     47 
     48 public:
     49     usage_error(const char*, ...) throw();
     50     ~usage_error(void) throw();
     51 
     52     const char* what(void) const throw();
     53 };
     54 
     55 // ------------------------------------------------------------------------
     56 // The "option" class.
     57 // ------------------------------------------------------------------------
     58 
     59 class option {
     60     char m_character;
     61     std::string m_argument;
     62     std::string m_description;
     63 
     64     friend class app;
     65 
     66 public:
     67     option(char, const std::string&, const std::string&);
     68 
     69     bool operator<(const option&) const;
     70 };
     71 
     72 // ------------------------------------------------------------------------
     73 // The "app" class.
     74 // ------------------------------------------------------------------------
     75 
     76 class app {
     77     bool m_hflag;
     78 
     79     void process_options(void);
     80     void usage(std::ostream&);
     81 
     82     bool inited(void);
     83 
     84 protected:
     85     typedef std::set< option > options_set;
     86 
     87     int m_argc;
     88     char* const* m_argv;
     89 
     90     const char* m_argv0;
     91     const char* m_prog_name;
     92     std::string m_description;
     93     std::string m_manpage, m_global_manpage;
     94 
     95     options_set options(void);
     96 
     97     // To be redefined.
     98     virtual std::string specific_args(void) const;
     99     virtual options_set specific_options(void) const;
    100     virtual void process_option(int, const char*);
    101     virtual int main(void) = 0;
    102 
    103 public:
    104     app(const std::string&, const std::string&, const std::string&);
    105     virtual ~app(void);
    106 
    107     int run(int, char* const*);
    108 };
    109 
    110 } // namespace application
    111 } // namespace tools
    112 
    113 #endif // !defined(TOOLS_APPLICATION_HPP)
    114