aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2005-05-10 23:48:35 +0000
committerMike Frysinger <vapier@gentoo.org>2005-05-10 23:48:35 +0000
commite5d0bde6975f3f599ea1123918e281db11e10517 (patch)
treea09ba17621d2c6e0f93bc73046fcfd031c94e3ee /libbb
parent38a33f91c0a46f51d07900731245492a3b244ae4 (diff)
downloadbusybox-w32-e5d0bde6975f3f599ea1123918e281db11e10517.tar.gz
busybox-w32-e5d0bde6975f3f599ea1123918e281db11e10517.tar.bz2
busybox-w32-e5d0bde6975f3f599ea1123918e281db11e10517.zip
add documentation for long options and touch up the current docs now that i actually understand how to use the function myself :)
Diffstat (limited to 'libbb')
-rw-r--r--libbb/getopt_ulflags.c101
1 files changed, 62 insertions, 39 deletions
diff --git a/libbb/getopt_ulflags.c b/libbb/getopt_ulflags.c
index 24bdcb653..86cf45325 100644
--- a/libbb/getopt_ulflags.c
+++ b/libbb/getopt_ulflags.c
@@ -28,6 +28,7 @@
28 28
29/* Documentation ! 29/* Documentation !
30 30
31unsigned long
31bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) 32bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
32 33
33 The command line options must be declared in const char 34 The command line options must be declared in const char
@@ -35,24 +36,25 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
35 36
36 flags = bb_getopt_ulflags(argc, argv, "rnug"); 37 flags = bb_getopt_ulflags(argc, argv, "rnug");
37 38
38 If one of the given options is found a flag value is added to 39 If one of the given options is found, a flag value is added to
39 the unsigned long returned by bb_getopt_ulflags. 40 the return value (an unsigned long).
40 41
41 The value of this flag is given by the position of the char in 42 The flag value is determined by the position of the char in
42 const char *applet_opts so for example in this case: 43 applet_opts string. For example, in the above case:
43 44
44 flags = bb_getopt_ulflags(argc, argv, "rnug"); 45 flags = bb_getopt_ulflags(argc, argv, "rnug");
45 46
46 "r" will add 1 47 "r" will add 1 (bit 1 : 0x01)
47 "n" will add 2 48 "n" will add 2 (bit 2 : 0x02)
48 "u will add 4 49 "u will add 4 (bit 3 : 0x03)
49 "g" will add 8 50 "g" will add 8 (bit 4 : 0x04)
50 51
51 and so on. 52 and so on. You can also look at the return value as a bit
53 field and each option sets one of bits.
52 54
53 If an argument is required by one of the options add a ":" 55 ":" If one of the options requires an argument, then add a ":"
54 after the char in const char *applet_opts and provide a pointer 56 after the char in applet_opts and provide a pointer to store
55 where the arg could be stored if it is found, for example: 57 the argument. For example:
56 58
57 char *pointer_to_arg_for_a; 59 char *pointer_to_arg_for_a;
58 char *pointer_to_arg_for_b; 60 char *pointer_to_arg_for_b;
@@ -63,20 +65,43 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
63 &pointer_to_arg_for_a, &pointer_to_arg_for_b, 65 &pointer_to_arg_for_a, &pointer_to_arg_for_b,
64 &pointer_to_arg_for_c, &pointer_to_arg_for_d); 66 &pointer_to_arg_for_c, &pointer_to_arg_for_d);
65 67
66 The type of the pointer (char* or llist_t *) can be influenced 68 The type of the pointer (char* or llist_t *) may be controlled
67 by the "*" special character that can be set in const char 69 by the "*" special character that is set in the external string
68 *bb_opt_complementaly (see below). 70 bb_opt_complementaly (see below for more info).
71
72static const struct option bb_default_long_options[]
73
74 This struct allows you to define long options. The syntax for
75 declaring the array is just like that of getopt's longopts.
76
77 static const struct option applet_long_options[] = {
78 { "verbose", 0, 0, "v" },
79 { 0, 0, 0, 0 }
80 };
81 bb_applet_long_options = applet_long_options;
82
83 The first parameter is the long option that you would pass to
84 the applet (--verbose) in place of the short option (-v). The
85 second field determines whether the option has an argument.
86 You can set this to 0, 1, or 2, or you can use the long named
87 defines of no_argument, required_argument, and optional_argument.
88 Just set the third argument to '0' or 'NULL'. The last argument
89 is simply the short option without the dash of course.
90
91 Note: a good applet will make long options configurable via the
92 config process and not a required feature. The current standard
93 is to name the config option CONFIG_FEATURE_<applet>_LONG_OPTIONS.
69 94
70const char *bb_opt_complementaly 95const char *bb_opt_complementaly
71 96
72 ":" The colon (":") is used in bb_opt_complementaly as separator 97 ":" The colon (":") is used to separate groups of two or more chars
73 between groups of two or more chars and/or groups of chars and 98 and/or groups of chars and special characters (stating some
74 special characters (stating some conditions to be checked). 99 conditions to be checked).
75 100
76 "abc" If groups of two or more chars are specified the first char 101 "abc" If groups of two or more chars are specified, the first char
77 is the main option and the other chars are secondary options 102 is the main option and the other chars are secondary options.
78 whose flags will be turned on if the main option is found even 103 Their flags will be turned on if the main option is found even
79 if they are not specifed on the command line, for example: 104 if they are not specifed on the command line. For example:
80 105
81 bb_opt_complementaly = "abc"; 106 bb_opt_complementaly = "abc";
82 107
@@ -92,30 +117,28 @@ Special characters:
92 to be unset (and ignored) if it is given on the command line. 117 to be unset (and ignored) if it is given on the command line.
93 118
94 For example: 119 For example:
95 The du applet can have the options "-s" and "-d depth", if 120 The du applet has the options "-s" and "-d depth". If
96 bb_getopt_ulflags finds -s then -d is unset or if it finds -d 121 bb_getopt_ulflags finds -s, then -d is unset or if it finds -d
97 then -s is unset. (Note: busybox implements the GNU 122 then -s is unset. (Note: busybox implements the GNU
98 "--max-depth" option as "-d".) In this case bb_getopt_ulflags's 123 "--max-depth" option as "-d".) To obtain this behavior, you
99 return value has no error flag set (0x80000000UL). To achieve 124 set bb_opt_complementaly = "s-d:d-s". Only one flag value is
100 this result you must set bb_opt_complementaly = "s-d:d-s". 125 added to bb_getopt_ulflags's return value depending on the
101 Only one flag value is added to bb_getopt_ulflags's return 126 position of the options on the command line. If one of the
102 value depending on the position of the options on the command 127 two options requires an argument pointer (":" in applet_opts
103 line. If one of the two options requires an argument pointer 128 as in "d:") optarg is set accordingly.
104 (":" in const char *applet_opts as in "d:") optarg is set
105 accordingly.
106 129
107 char *smax_print_depth; 130 char *smax_print_depth;
108 131
109 bb_opt_complementaly = "s-d:d-s"; 132 bb_opt_complementaly = "s-d:d-s";
110 opt = bb_getopt_ulflags(argc, argv, "sd:" , &smax_print_depth); 133 opt = bb_getopt_ulflags(argc, argv, "sd:", &smax_print_depth);
111 134
112 if (opt & 2) { 135 if (opt & 2) {
113 max_print_depth = bb_xgetularg10_bnd(smax_print_depth, 136 max_print_depth = bb_xgetularg10_bnd(smax_print_depth,
114 0, INT_MAX); 137 0, INT_MAX);
115 } 138 }
116 139
117 "~" A tilde between two options or between an option and a group 140 "~" A tilde between two options, or between an option and a group
118 of options means that they are mutually exclusive. Unlike 141 of options, means that they are mutually exclusive. Unlike
119 the "-" case above, an error will be forced if the options 142 the "-" case above, an error will be forced if the options
120 are used together. 143 are used together.
121 144
@@ -128,7 +151,7 @@ Special characters:
128 return value will have the error flag set (0x80000000UL) so 151 return value will have the error flag set (0x80000000UL) so
129 that we can check for it: 152 that we can check for it:
130 153
131 if ((flags & 0x80000000UL) 154 if (flags & 0x80000000UL)
132 bb_show_usage(); 155 bb_show_usage();
133 156
134 "*" A star after a char in bb_opt_complementaly means that the 157 "*" A star after a char in bb_opt_complementaly means that the
@@ -138,15 +161,15 @@ Special characters:
138 The grep applet can have one or more "-e pattern" arguments. 161 The grep applet can have one or more "-e pattern" arguments.
139 In this case you should use bb_getopt_ulflags() as follows: 162 In this case you should use bb_getopt_ulflags() as follows:
140 163
141 llist_t *patterns=NULL; 164 llist_t *patterns = NULL;
142 165
143 (this pointer must be initializated to NULL if the list is empty 166 (this pointer must be initializated to NULL if the list is empty
144 as required by *llist_add_to(llist_t *old_head, char *new_item).) 167 as required by *llist_add_to(llist_t *old_head, char *new_item).)
145 168
146 bb_opt_complementaly = "e*"; 169 bb_opt_complementaly = "e*";
147 170
148 bb_getopt_ulflags (argc, argv, "e:", &patterns); 171 bb_getopt_ulflags(argc, argv, "e:", &patterns);
149 grep -e user -e root /etc/passwd 172 $ grep -e user -e root /etc/passwd
150 root:x:0:0:root:/root:/bin/bash 173 root:x:0:0:root:/root:/bin/bash
151 user:x:500:500::/home/user:/bin/bash 174 user:x:500:500::/home/user:/bin/bash
152 175