1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <getopt.h>
4 #include <stdio.h>
5 
6 #include "alloc-util.h"
7 #include "pretty-print.h"
8 #include "service-util.h"
9 #include "terminal-util.h"
10 #include "util.h"
11 
help(const char * program_path,const char * service,const char * description,bool bus_introspect)12 static int help(const char *program_path, const char *service, const char *description, bool bus_introspect) {
13         _cleanup_free_ char *link = NULL;
14         int r;
15 
16         r = terminal_urlify_man(service, "8", &link);
17         if (r < 0)
18                 return log_oom();
19 
20         printf("%s [OPTIONS...]\n\n"
21                "%s%s%s\n\n"
22                "This program takes no positional arguments.\n\n"
23                "%sOptions%s:\n"
24                "  -h --help                 Show this help\n"
25                "     --version              Show package version\n"
26                "     --bus-introspect=PATH  Write D-Bus XML introspection data\n"
27                "\nSee the %s for details.\n"
28                , program_path
29                , ansi_highlight(), description, ansi_normal()
30                , ansi_underline(), ansi_normal()
31                , link
32         );
33 
34         return 0; /* No further action */
35 }
36 
service_parse_argv(const char * service,const char * description,const BusObjectImplementation * const * bus_objects,int argc,char * argv[])37 int service_parse_argv(
38                 const char *service,
39                 const char *description,
40                 const BusObjectImplementation* const* bus_objects,
41                 int argc, char *argv[]) {
42 
43         enum {
44                 ARG_VERSION = 0x100,
45                 ARG_BUS_INTROSPECT,
46         };
47 
48         static const struct option options[] = {
49                 { "help",           no_argument,       NULL, 'h'                },
50                 { "version",        no_argument,       NULL, ARG_VERSION        },
51                 { "bus-introspect", required_argument, NULL, ARG_BUS_INTROSPECT },
52                 {}
53         };
54 
55         int c;
56 
57         assert(argc >= 0);
58         assert(argv);
59 
60         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
61                 switch (c) {
62 
63                 case 'h':
64                         return help(argv[0], service, description, bus_objects);
65 
66                 case ARG_VERSION:
67                         return version();
68 
69                 case ARG_BUS_INTROSPECT:
70                         return bus_introspect_implementations(
71                                         stdout,
72                                         optarg,
73                                         bus_objects);
74 
75                 case '?':
76                         return -EINVAL;
77 
78                 default:
79                         assert_not_reached();
80                 }
81 
82         if (optind < argc)
83                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
84                                        "This program takes no arguments.");
85 
86         return 1; /* Further action */
87 }
88