2020-11-09 13:23:58 +09:00
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2020-04-23 10:19:11 +02:00
# include <getopt.h>
# include <stdio.h>
# include "alloc-util.h"
2022-11-06 16:30:58 +01:00
# include "build.h"
2020-04-23 10:19:11 +02:00
# include "pretty-print.h"
# include "service-util.h"
# include "terminal-util.h"
2020-04-28 17:55:34 +02:00
static int help ( const char * program_path , const char * service , const char * description , bool bus_introspect ) {
2020-04-23 10:19:11 +02:00
_cleanup_free_ char * link = NULL ;
int r ;
r = terminal_urlify_man ( service , " 8 " , & link ) ;
if ( r < 0 )
return log_oom ( ) ;
printf ( " %s [OPTIONS...] \n \n "
" %s%s%s \n \n "
" This program takes no positional arguments. \n \n "
" %sOptions%s: \n "
" -h --help Show this help \n "
" --version Show package version \n "
2020-04-28 17:55:34 +02:00
" --bus-introspect=PATH Write D-Bus XML introspection data \n "
2020-04-23 10:19:11 +02:00
" \n See the %s for details. \n "
, program_path
, ansi_highlight ( ) , description , ansi_normal ( )
, ansi_underline ( ) , ansi_normal ( )
, link
) ;
return 0 ; /* No further action */
}
int service_parse_argv (
const char * service ,
const char * description ,
2020-04-28 17:55:34 +02:00
const BusObjectImplementation * const * bus_objects ,
2020-04-23 10:19:11 +02:00
int argc , char * argv [ ] ) {
enum {
ARG_VERSION = 0x100 ,
2020-04-28 17:55:34 +02:00
ARG_BUS_INTROSPECT ,
2020-04-23 10:19:11 +02:00
} ;
static const struct option options [ ] = {
{ " help " , no_argument , NULL , ' h ' } ,
{ " version " , no_argument , NULL , ARG_VERSION } ,
2020-04-28 17:55:34 +02:00
{ " bus-introspect " , required_argument , NULL , ARG_BUS_INTROSPECT } ,
2020-04-23 10:19:11 +02:00
{ }
} ;
int c ;
assert ( argc > = 0 ) ;
assert ( argv ) ;
while ( ( c = getopt_long ( argc , argv , " h " , options , NULL ) ) > = 0 )
2022-04-01 22:37:21 +09:00
switch ( c ) {
2020-04-23 10:19:11 +02:00
case ' h ' :
2020-04-28 17:55:34 +02:00
return help ( argv [ 0 ] , service , description , bus_objects ) ;
2020-04-23 10:19:11 +02:00
case ARG_VERSION :
return version ( ) ;
2020-04-28 17:55:34 +02:00
case ARG_BUS_INTROSPECT :
return bus_introspect_implementations (
stdout ,
optarg ,
bus_objects ) ;
2020-04-23 10:19:11 +02:00
case ' ? ' :
return - EINVAL ;
default :
Drop the text argument from assert_not_reached()
In general we almost never hit those asserts in production code, so users see
them very rarely, if ever. But either way, we just need something that users
can pass to the developers.
We have quite a few of those asserts, and some have fairly nice messages, but
many are like "WTF?" or "???" or "unexpected something". The error that is
printed includes the file location, and function name. In almost all functions
there's at most one assert, so the function name alone is enough to identify
the failure for a developer. So we don't get much extra from the message, and
we might just as well drop them.
Dropping them makes our code a tiny bit smaller, and most importantly, improves
development experience by making it easy to insert such an assert in the code
without thinking how to phrase the argument.
2021-07-27 12:27:28 +02:00
assert_not_reached ( ) ;
2020-04-23 10:19:11 +02:00
}
if ( optind < argc )
return log_error_errno ( SYNTHETIC_ERRNO ( EINVAL ) ,
" This program takes no arguments. " ) ;
return 1 ; /* Further action */
}