chiark / gitweb /
bin/disorder-notify: Add a proper command-line parser, and support `-u'.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 10 Jun 2020 15:05:46 +0000 (16:05 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 10 Jun 2020 15:05:46 +0000 (16:05 +0100)
bin/disorder-notify

index 49e83f0ba5745879ebac88f7a04b1bdf64da8747..91278f0e6ca7b6f5b4c7e4edc78fee5b2c3057a9 100755 (executable)
@@ -5,6 +5,8 @@ use strict;
 
 use DisOrder;
 use File::FcntlLock;
+use Getopt::Long qw{:config gnu_compat bundling
+                   require_order no_getopt_compat};
 use POSIX qw{:errno_h :fcntl_h};
 
 ###--------------------------------------------------------------------------
@@ -14,6 +16,7 @@ my %C = (config => "$ENV{HOME}/.disorder/passwd",
         lockdir => "$ENV{HOME}/.disorder/",
         mixer => "Master,0");
 
+(my $PROG = $0) =~ s:^.*/::;
 my $TITLE = "DisOrder";
 my $VARIANT = "default";
 if (-l $C{config} && (my $t = readlink $C{config}) =~ /^passwd\.(.*)$/)
@@ -358,7 +361,39 @@ $OP{"next-config"} = sub {
 ###--------------------------------------------------------------------------
 ### Main program.
 
-if (@ARGV != 1) { print STDERR "usage: $0 OP\n"; exit 2; }
+sub usage (\*) {
+  my ($fh) = @_;
+  print $fh "usage: $PROG [-u CONFIG] COMMAND\n";
+}
+
+sub help () {
+  usage *STDOUT;
+  print <<EOF;
+
+Command-line options:
+  -h, --help           Show this help text
+  -u, --user-config    Set user configuration file
+
+Commands:
+  volume-up
+  volume-down
+  scratch
+  enable/disable
+  play/pause
+  watch
+  now-playing
+  notify-now-playing
+  next-config
+EOF
+}
+
+my $bad = 0;
+GetOptions
+  "h|help" => sub { help; exit 0; },
+  "u|user-config=s" => \$C{config}
+    or $bad = 1;
+@ARGV == 1 or $bad = 1;
+if ($bad) { usage *STDERR; exit 2; }
 my $op = $ARGV[0];
 if (!exists $OP{$op}) { print STDERR "$0: unknown op `$op'\n"; exit 2; }
 $OP{$op}();