feisty meow concerns codebase  2.140
command_line.h
Go to the documentation of this file.
1 #ifndef COMMAND_LINE_CLASS
2 #define COMMAND_LINE_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : command_line *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 1992-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
18 #include <basis/contracts.h>
19 #include <filesystem/filename.h>
20 
21 namespace application {
22 
24 
45 // forward declarations.
46 class internal_cmd_line_array_of_parms;
47 
48 class command_parameter : public virtual basis::root_object
49 {
50 public:
52 
55 
58 
61  command_parameter(const command_parameter &to_copy);
62 
64 
65  DEFINE_CLASS_NAME("command_parameter");
66 
68 
69  parameter_types type() const { return _type; }
71  void type(parameter_types new_type) { _type = new_type; }
73 
74  const basis::astring &text() const;
76  void text(const basis::astring &new_text);
78 
79 private:
80  parameter_types _type;
81  basis::astring *_text;
82 };
83 
85 
87 {
88 public:
89  command_line(int argc, char *argv[]);
91 
98  command_line(const basis::astring &to_parse);
100 
105  virtual ~command_line();
106 
107  DEFINE_CLASS_NAME("command_line");
108 
111 
112  static void separate_command_line(const basis::astring &cmd_line, basis::astring &app,
113  basis::astring &parms);
115 
123  int entries() const;
125 
128  const command_parameter &get(int field) const;
130 
133  bool zap(int field);
135 
138  // note: in the following, if "case_sense" is true, then the searches are
139  // case-sensitive. otherwise, case of the flags is not a concern.
140  // the returned values always retain the original case.
141 
142  bool find(char option_character, int &index, bool case_sense = true) const;
144 
147  bool find(const basis::astring &option_string, int &index,
148  bool case_sense = true) const;
150 
151  bool get_value(char option_character, basis::astring &value,
152  bool case_sense = true) const;
154 
159  bool get_value(const basis::astring &option_string, basis::astring &value,
160  bool case_sense = true) const;
162 
163 //is this useful? it's kind of like what we need for special flags (like
164 // -fgob.h, where gob.h is a value parameter) but needs to terminate
165 //differently for that to work.
166  basis::astring gather(int &index) const;
168 
178  basis::astring text_form() const;
180 
183 
187 private:
188  internal_cmd_line_array_of_parms *_implementation;
189  filesystem::filename *_program_name;
190 
191  void parse_string_array(const structures::string_array &to_parse);
193 
194  // forbidden:
195  command_line(const command_line &to_copy);
196  command_line &operator =(const command_line &to_copy);
197 
198  static const command_parameter &cmdline_blank_parm();
199 };
200 
202 
203 // this declares a program-wide command-line argument storage area.
204 
205 extern int _global_argc;
206 extern char **_global_argv;
208 #define DEFINE_ARGC_AND_ARGV int _global_argc = 0; char **_global_argv = NULL_POINTER
209 
211 #define SET_ARGC_ARGV(argc, argv) { \
212  application::_global_argc = argc; \
213  application::_global_argv = argv; \
214 }
215 
216 } //namespace.
217 
218 #endif
219 
int entries() const
Returns the number of fields found on the command line.
static structures::string_array get_command_line()
returns the command line passed to the program as a list of strings.
filesystem::filename program_name() const
Returns the program name found in the command line.
static void separate_command_line(const basis::astring &cmd_line, basis::astring &app, basis::astring &parms)
breaks apart a command line in "cmd_line" into "app" and "parms".
bool find(char option_character, int &index, bool case_sense=true) const
Returns true if the "option_character" is found in the parameters.
bool get_value(char option_character, basis::astring &value, bool case_sense=true) const
retrieves the "value" found for the option flag specified.
const command_parameter & get(int field) const
Returns the parameter at the "field" specified.
basis::astring text_form() const
returns a string with all the information we have for the command line.
basis::astring gather(int &index) const
coalesces parameters together until the next option flag.
bool zap(int field)
eats the entry at position "field".
command_line(int argc, char *argv[])
takes command line parameters in the form of "argc" and "argv".
DEFINE_CLASS_NAME("command_line")
const basis::astring & text() const
observes the string contents.
command_parameter(parameter_types type=BOGUS_ITEM)
default constructor initializes to mostly blank state.
DEFINE_CLASS_NAME("command_parameter")
command_parameter & operator=(const command_parameter &to_copy)
parameter_types type() const
observes the type of the parameter.
Definition: command_line.h:69
void type(parameter_types new_type)
modifies the type of the parameter.
Definition: command_line.h:71
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
Provides operations commonly needed on file names.
Definition: filename.h:64
An array of strings with some additional helpful methods.
Definition: string_array.h:32
Implements an application lock to ensure only one is running at once.
char ** _global_argv