]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/getopt-long.hh
8233114ed7674858734ca540e572fca7893a92d7
[lilypond.git] / flower / include / getopt-long.hh
1 #ifndef GETOPT_LONG_HH
2 #define GETOPT_LONG_HH
3
4 #include <cstdio>
5
6 #include "string.hh"
7
8 /**
9    a struct this for initialising the commandline options.
10 */
11 struct Long_option_init {
12   char const * take_arg_str0_;
13   char const * longname_str0_;
14
15   /*
16     = 0: don't take short version.
17   */
18   char shortname_char_;
19
20   char const * help_str0_;
21   
22   String to_string () const;
23   String str_for_help () const;
24   //   NO constructor!
25
26   static int compare (Long_option_init const&,Long_option_init const&);
27   static String table_string (Long_option_init *); 
28 };
29
30
31 /** C++ for version of long_getopt.  For processing GNU style command
32   line arguments.  No pointer (return values, arguments) contents are
33   copied.
34   
35   TODO: handle 
36   command  - , and command --
37   
38   argument reordering
39   */
40 class Getopt_long {
41
42   /// the option info.
43   const Long_option_init *option_a_;
44   int table_len_;
45     
46   /// if doing short option, arg_value_char_a_a_[optind][optindind] is processed next.
47   int argument_index_;
48
49   /// the option found
50   const Long_option_init *found_option_;
51
52
53 public: 
54   /** errorcodes: no error, argument expected, no argument expected,
55     unknown option, illegal argument (eg. int expected).  */
56   enum Errorcod { E_NOERROR = 0, E_ARGEXPECT, E_NOARGEXPECT, E_UNKNOWNOPTION,
57                   E_ILLEGALARG } ;
58
59   /// argument. Set to 0 if not present
60   char const * optional_argument_str0_;
61
62   /// current error status
63   Errorcod error_;
64
65   /// arg_value_char_a_a_[array_index_] will be processed next.
66   int array_index_;
67
68   /// the arguments
69   char **arg_value_char_a_a_;
70
71   /// the arg. count
72   int argument_count_;
73
74   FILE *error_out_;
75
76 public:
77   /// get ready for processing next error.
78   void next ();
79   const Long_option_init *parselong ();
80   const Long_option_init *parseshort ();
81   void OK () const;
82   bool ok () const;
83
84   /// report an error and abort
85   void report (Errorcod c);
86
87
88   /// return an integer (with err. detect)
89   long get_argument_index ();
90  
91     
92   /**
93     What to do with  errors. 
94     report messages on  #*os#, and abort.
95     if #os# is null, then do not report nor abort, just set #error#
96     */
97        
98   void seterror (FILE *os);
99
100   /// construct: pass arguments and option info.
101   Getopt_long (int c,  char **v, Long_option_init *lo);
102
103   /**  get the next option. 
104     @return pointer to next option found.
105     0 if error occurred, or next argument is no option.
106     */
107   const Long_option_init *operator () ();
108
109   char const *current_arg ();
110   char const * get_next_arg ();
111 };
112
113 #endif // GETOPT_LONG_HH