Laxkit  0.0.7.1
laxoptions.h
1 //
2 //
3 // The Laxkit, a windowing toolkit
4 // Please consult http://laxkit.sourceforge.net about where to send any
5 // correspondence about this software.
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library General Public License for more details.
16 //
17 // You should have received a copy of the GNU Library General Public
18 // License along with this library; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 //
21 // Copyright (C) 2011 by Tom Lechner
22 //
23 #ifndef _LAX_LAXOPTIONS_H
24 #define _LAX_LAXOPTIONS_H
25 
26 #include <cstdio>
27 #include <lax/lists.h>
28 
29 namespace Laxkit {
30 
31 
32 //-------------------------------------- LaxOption -------------------------------------
33 
34 #define LAXOPT_Hidden (1<<0)
35 #define LAXOPT_HasParam (1<<1)
36 #define LAXOPT_Group (1<<2)
37 #define LAXOPT_Remaining (1<<3)
38 #define LAXOPT_Unknown (1<<4)
39 #define LAXOPT_MissingParam (1<<5)
40 
41 class LaxOption
42 {
43  public:
44  char *help_text;
45  char *long_option;
46  int short_option;
47  int flags;
48  int option_id;
49  int groupnumber;
50  char *short_example;
51  char *option_example; // like '--list, -l "all"' instead of just "--list, -l"
52 
53  int parsed_present;
54  char *parsed_arg;
55 
56  LaxOption(const char *l_option,
57  int s_option,
58  int nflags,
59  const char *nhelp_text,
60  int nid,
61  const char *opt_example, // like '--list, -l "all"' instead of just "--list, -l"
62  int ex_is_full=0
63  );
64  ~LaxOption();
65  int unknown() { return flags&LAXOPT_Unknown; }
66  int missing_param() { return flags&LAXOPT_MissingParam; }
67  int has_arg() { return flags&LAXOPT_HasParam; }
68  int chr() { return short_option; }
69  const char *str() { return long_option; }
70  int id() { return option_id; }
71  int present() { return parsed_present; }
72  const char *helptext() { return help_text; }
73  const char *arg();
74 
75 };
76 
77 
78 //-------------------------------------- LaxOptions -------------------------------------
79 class LaxOptions : protected PtrStack<LaxOption>
80 {
81  protected:
82  char *helpheader;
83  char *optionsheader;
84  char *usageline;
85 
86  int curgroup, curitem;
87  int loop_which;
88  int first_remaining;
89  int erred;
90  public:
91  LaxOptions();
92  ~LaxOptions();
93 
94  int Verify();
95  void HelpHeader(const char *header_text);
96  const char *HelpHeader() { return (helpheader?helpheader:"Help!"); }
97  void UsageLine(const char *usage_line);
98  void OptionsHeader(const char *usage_line);
99  int NewGroup(const char *group_header_text);
100  int Add(const char *long_option,
101  int short_option,
102  int has_param,
103  const char *nhelp_text,
104  int nid=0,
105  const char *opt_example=NULL // like '--list, -l "all"' instead of just "--list, -l"
106  );
107 
108  void Help(FILE *file=NULL, int columns=-1);
109  void HelpHtml(FILE *file=NULL);
110  void HelpMan(FILE *file=NULL);
111  int Parse(int argc, char **argv, int *argerr);
112  int Review(int which); //1 for remaining, 0 for options
113  LaxOption *error();
114  LaxOption *start();
115  LaxOption *remaining();
116  LaxOption *next();
117  LaxOption *find(const char *long_option, int short_option);
118  int more(); //return number of remaining options
119 
120  //void man_page_text(); //print to stdout the options formatted for a man page
121 };
122 
123 } //namespace Laxkit
124 
125 
126 
127 #endif
128 

Mon Feb 17 2014 11:52:56, Laxkit