]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/arguments.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
8 * Michel Normand <normand at fr.ibm.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <ctype.h> /* for isprint() */
31 #include <sys/types.h>
34 #include "arguments.h"
36 /*---------------------------------------------------------------------------*/
37 static int build_shortopts(const struct option
*a_options
,
38 char *a_shortopts
, size_t a_size
)
40 const struct option
*opt
;
43 if (!a_options
|| !a_shortopts
|| !a_size
)
46 for (opt
= a_options
; opt
->name
; opt
++) {
48 if (!isascii(opt
->val
))
52 a_shortopts
[i
++] = opt
->val
;
56 if (opt
->has_arg
== no_argument
)
60 a_shortopts
[i
++] = ':';
64 if (opt
->has_arg
== required_argument
)
68 a_shortopts
[i
++] = ':';
74 a_shortopts
[i
] = '\0';
85 /*---------------------------------------------------------------------------*/
86 static void print_usage(const struct option longopts
[],
87 const struct lxc_arguments
*a_args
)
91 const struct option
*opt
;
93 fprintf(stderr
, "Usage: %s ", a_args
->progname
);
95 for (opt
= longopts
, i
= 1; opt
->name
; opt
++, i
++) {
97 char *uppername
= strdup(opt
->name
);
102 for (j
= 0; uppername
[j
]; j
++)
103 uppername
[j
] = toupper(uppername
[j
]);
105 fprintf(stderr
, "[");
107 if (isprint(opt
->val
))
108 fprintf(stderr
, "-%c|", opt
->val
);
110 fprintf(stderr
, "--%s", opt
->name
);
112 if (opt
->has_arg
== required_argument
)
113 fprintf(stderr
, "=%s", uppername
);
115 if (opt
->has_arg
== optional_argument
)
116 fprintf(stderr
, "[=%s]", uppername
);
118 fprintf(stderr
, "] ");
121 fprintf(stderr
, "\n\t");
126 fprintf(stderr
, "\n");
130 static void print_help(const struct lxc_arguments
*args
, int code
)
136 -o, --logfile=FILE Output log to FILE instead of stderr\n\
137 -l, --logpriority=LEVEL Set log priority to LEVEL\n\
138 -q, --quiet Don't produce any output\n\
139 -?, --help Give this help list\n\
140 --usage Give a short usage message\n\
142 Mandatory or optional arguments to long options are also mandatory or optional\n\
143 for any corresponding short options.\n\
145 See the %s man page for further information.\n\n",
146 args
->progname
, args
->help
, args
->progname
);
151 extern int lxc_arguments_parse(struct lxc_arguments
*args
,
152 int argc
, char * const argv
[])
157 ret
= build_shortopts(args
->options
, shortopts
, sizeof(shortopts
));
159 lxc_error(args
, "build_shortopts() failed : %s",
167 c
= getopt_long(argc
, argv
, shortopts
, args
->options
, &index
);
171 case 'n': args
->name
= optarg
; break;
172 case 'o': args
->log_file
= optarg
; break;
173 case 'l': args
->log_priority
= optarg
; break;
174 case 'c': args
->console
= optarg
; break;
175 case 'q': args
->quiet
= 1; break;
176 case OPT_USAGE
: print_usage(args
->options
, args
);
177 case '?': print_help(args
, 1);
178 case 'h': print_help(args
, 0);
181 ret
= args
->parser(args
, c
, optarg
);
189 * Reclaim the remaining command arguments
191 args
->argv
= &argv
[optind
];
192 args
->argc
= argc
- optind
;
194 /* Check the command options */
197 lxc_error(args
, "missing container name, use --name option");
202 ret
= args
->checker(args
);
205 lxc_error(args
, "could not parse command line");
209 int lxc_arguments_str_to_int(struct lxc_arguments
*args
, const char *str
)
215 val
= strtol(str
, &endptr
, 10);
217 lxc_error(args
, "invalid statefd '%s' : %m", str
);
222 lxc_error(args
, "invalid digit for statefd '%s'", str
);