15
15
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
*
17
17
* ChangeLog:
18
+ * 2007-06-01 Nick <nickishappy@gmail.com>
19
+ * * reworked command-line options (added standard options, long options)
20
+ *
18
21
* 2007-04-09 L. Donnie Smith <cwiid@abstrakraft.org>
19
22
* * updated for libcwiid rename
20
23
*
31
34
#include <unistd.h>
32
35
#include <bluetooth/bluetooth.h>
33
36
#include <cwiid.h>
37
+ #include <getopt.h>
34
38
35
- #define OPTSTRING "ahlq"
36
-
37
- #define USAGE \
38
- "%s [OPTIONS]\n" \
39
- " -a list all bluetooth devices (not just wiimotes)\n" \
40
- " -h print this help message\n" \
41
- " -l long format (device details)\n" \
42
- " -q quiet mode\n"
39
+ void print_usage (void )
40
+ {
41
+ printf ("lswm lists availiable wiimotes\n" );
42
+ printf ("Usage: %s [OPTIONS]...\n\n" , "lswm" );
43
+ printf ("Options:\n" );
44
+ printf ("\t-h, --help\t\tPrints this output.\n" );
45
+ printf ("\t-v, --version\t\toutput version information and exit.\n" );
46
+ printf ("\t-l, --long\t\tlong format (device details).\n" );
47
+ printf ("\t-q, --quiet\t\tquiet mode (less verbose).\n" );
48
+ printf ("\t-a, --all\t\tlist all bluetooth devices (not just wiimotes).\n" );
49
+ }
43
50
44
51
int main (int argc , char * argv [])
45
52
{
@@ -53,10 +60,27 @@ int main(int argc, char *argv[])
53
60
char ba_str [18 ];
54
61
55
62
/* Parse options */
56
- while ((c = getopt (argc , argv , OPTSTRING )) != -1 ) {
63
+ while (1 ) {
64
+ int option_index = 0 ;
65
+
66
+ static struct option long_options [] = {
67
+ {"help" , 0 , 0 , 'h' },
68
+ {"all" , 0 , 0 , 'a' },
69
+ {"long" , 0 , 0 , 'l' },
70
+ {"version" , 0 , 0 , 'v' },
71
+ {"quiet" , 0 , 0 , 'q' },
72
+ {0 , 0 , 0 , 0 }
73
+ };
74
+
75
+ c = getopt_long (argc , argv , "halvq" , long_options , & option_index );
76
+
77
+ if (c == -1 ) {
78
+ break ;
79
+ }
80
+
57
81
switch (c ) {
58
82
case 'h' :
59
- printf ( USAGE , argv [ 0 ] );
83
+ print_usage ( );
60
84
return 0 ;
61
85
break ;
62
86
case 'a' :
@@ -65,19 +89,21 @@ int main(int argc, char *argv[])
65
89
case 'l' :
66
90
long_format = 1 ;
67
91
break ;
92
+ case 'v' :
93
+ printf ("CWiid Version %s\n" , CWIID_VERSION );
94
+ return 0 ;
95
+ break ;
68
96
case 'q' :
69
97
quiet = 1 ;
70
98
break ;
71
99
case '?' :
72
100
default :
73
- fprintf (stderr , "Try '%s -h ' for more information\n" , argv [0 ]);
101
+ fprintf (stderr , "Try '%s --help ' for more information\n" , argv [0 ]);
74
102
return -1 ;
75
103
break ;
76
104
}
77
105
}
78
106
79
- /* TODO: check for other stuff on the command-line? */
80
-
81
107
/* Handle quiet mode */
82
108
if (quiet ) {
83
109
cwiid_set_err (NULL );
@@ -111,4 +137,3 @@ int main(int argc, char *argv[])
111
137
112
138
return 0 ;
113
139
}
114
-
0 commit comments