Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix commandline handling #1208

Merged
merged 7 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contrib/_dunst.zshcomp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ case $state in
{-v,--version,-version}"[Print version]" \
'(-verbosity)-verbosity[Minimum level for message]' \
{-conf,--config}"[Path to configuration file]:file:_files" \
{-print,--print}"[Print notifications to stdout]" \
{-startup_notification,--startup_notification}"[Display a notification on startup]" \
{-h,-help,--help}"[Print help]"
;;

Expand Down
2 changes: 1 addition & 1 deletion contrib/dunst.bashcomp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ _dunst() {
local opts cur prev split=false
_get_comp_words_by_ref cur prev
COMPREPLY=()
opts='-v -version --version -verbosity -conf -config -h --help'
opts='-v -version --version -verbosity -conf -config -print --print -startup_notification --startup_notification -h -help --help'

case "$prev" in
-verbosity) COMPREPLY=("crit" "warn" "mesg" "info" "debug")
Expand Down
10 changes: 5 additions & 5 deletions docs/dunst.1.pod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dunst - A customizable and lightweight notification-daemon

=head1 SYNOPSIS

dunst [-conf file] [-verbosity v] [-print] [--startup-notification]
dunst [-conf file] [-verbosity v] [-print] [--startup_notification]

=head1 DESCRIPTION

Expand All @@ -23,7 +23,7 @@ systemd service.

=over 4

=item B<-h/--help>
=item B<-h/-help/--help>

List all command line flags

Expand All @@ -36,7 +36,7 @@ defaults.
(Hint: `dunst -conf - </dev/null` can be used to enforce the defaults, i.e. for
testing)

=item B<-v/--version>
=item B<-v/-version/--version>

Print version information.

Expand All @@ -46,12 +46,12 @@ Do not display log messages, which have lower precedence than specified
verbosity. This won't affect printing notifications on the terminal. Use
the '-print' option for this.

=item B<-print>
=item B<-print/--print>

Print notifications to stdout. This might be useful for logging, setting up
rules or using the output in other scripts.

=item B<--startup_notification> (values: [true/false], default: false)
=item B<-startup_notification/--startup_notification>

Display a notification on startup.

Expand Down
20 changes: 9 additions & 11 deletions src/dunst.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ int dunst_main(int argc, char *argv[])

dunst_log_init(false);

if (cmdline_get_bool("-v/-version", false, "Print version")
|| cmdline_get_bool("--version", false, "Print version")) {
if (cmdline_get_bool("-v/-version/--version", false, "Print version")) {
print_version();
}

Expand All @@ -180,18 +179,17 @@ int dunst_main(int argc, char *argv[])
"Path to configuration file");
load_settings(cmdline_config_path);

if (cmdline_get_bool("-h/-help", false, "Print help")
|| cmdline_get_bool("--help", false, "Print help")) {
usage(EXIT_SUCCESS);
}

if (cmdline_get_bool("-print", false, "Print notifications to stdout")
|| cmdline_get_bool("--print", false, "Print notifications to stdout")) {
if (cmdline_get_bool("-print/--print", false, "Print notifications to stdout")) {
settings.print_notifications = true;
}

settings.startup_notification = cmdline_get_bool("--startup_notification",
0, "Display a notification on startup.");
settings.startup_notification = cmdline_get_bool("-startup_notification/--startup_notification",
false, "Display a notification on startup.");

/* Help should always be the last to set up as calls to cmdline_get_* (as a side effect) add entries to the usage list. */
if (cmdline_get_bool("-h/-help/--help", false, "Print help")) {
usage(EXIT_SUCCESS);
}

int dbus_owner_id = dbus_init();

Expand Down
33 changes: 9 additions & 24 deletions src/option_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,33 +482,18 @@ int cmdline_find_option(const char *key)
{
ASSERT_OR_RET(key, -1);

char *key1 = g_strdup(key);
char *key2 = strchr(key1, '/');
gchar **keys = g_strsplit(key, "/", -1);

if (key2) {
*key2 = '\0';
key2++;
}

/* look for first key */
for (int i = 0; i < cmdline_argc; i++) {
if (STR_EQ(key1, cmdline_argv[i])) {
g_free(key1);
return i;
}
}

/* look for second key if one was specified */
if (key2) {
for (int i = 0; i < cmdline_argc; i++) {
if (STR_EQ(key2, cmdline_argv[i])) {
g_free(key1);
return i;
for (int i = 0; keys[i] != NULL; i++) {
for (int j = 0; j < cmdline_argc; j++) {
if (STR_EQ(keys[i], cmdline_argv[j])) {
g_strfreev(keys);
return j;
}
}
}

g_free(key1);
g_strfreev(keys);
return -1;
}

Expand Down Expand Up @@ -623,14 +608,14 @@ void cmdline_usage_append(const char *key, const char *type, const char *descrip

if (!usage_str) {
usage_str =
g_strdup_printf("%-40s - %s\n", key_type, description);
g_strdup_printf("%-50s - %s\n", key_type, description);
g_free(key_type);
return;
}

char *tmp;
tmp =
g_strdup_printf("%s%-40s - %s\n", usage_str, key_type, description);
g_strdup_printf("%s%-50s - %s\n", usage_str, key_type, description);
g_free(key_type);

g_free(usage_str);
Expand Down
Loading