Skip to content

Commit 75df187

Browse files
mikkoioalders
authored andcommitted
Add Bash completion, get opts from mech-dump
1 parent 0d65e53 commit 75df187

2 files changed

Lines changed: 33 additions & 32 deletions

File tree

script/mech-dump

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ my $agent;
2020
my $agent_alias;
2121
my $cookie_filename;
2222

23-
GetOptions(
23+
my %command_line_options = (
2424
'user=s' => \$user,
2525
'password=s' => \$pass,
2626
headers => sub { push( @actions, \&dump_headers ) },
@@ -35,14 +35,37 @@ GetOptions(
3535
);
3636
},
3737
text => sub { push( @actions, \&dump_text ) },
38-
absolute => \$absolute,
38+
'absolute!' => \$absolute,
3939
'agent=s' => \$agent,
4040
'agent-alias=s' => \$agent_alias,
4141
'cookie-file=s' => \$cookie_filename,
4242
help => sub { pod2usage(1); },
43-
version => sub { print STDERR $WWW::Mechanize::VERSION, "\n"; exit 0; },
43+
version => sub { print STDERR $WWW::Mechanize::VERSION, "\n"; exit 0; },
44+
'completions' => sub { completions(@ARGV); },
45+
);
46+
GetOptions(
47+
%command_line_options
4448
) or pod2usage(2);
4549

50+
sub completions {
51+
my (@words) = @_;
52+
my @opts;
53+
foreach (sort keys %command_line_options) {
54+
if(m/^ (?<opt> [^!]{1,}) ! $/msx) {
55+
push @opts, $+{opt}, 'no-'.$+{opt};
56+
}
57+
elsif(m/^ (?<opt> [^=]{1,}) = [siof]{1} $/msx) {
58+
push @opts, (split qr{\|}, $+{opt});
59+
}
60+
else {
61+
push @opts, $_;
62+
}
63+
}
64+
print join "\n", (map { q{--} . $_ } @opts);
65+
print "\n";
66+
exit 0;
67+
}
68+
4669
=head1 SYNOPSIS
4770
4871
mech-dump [options] [file|url]
Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,12 @@
1-
#!/usr/bin/env bash
2-
3-
__mech_dump_init_completion() {
4-
COMPREPLY=()
5-
_get_comp_words_by_ref cur prev words cword
6-
}
7-
__mech_dump_command_line_options() {
8-
opts=(
9-
--headers --forms --links --images --all --test
10-
--user= --password= --cookie-file= --agent
11-
--agent-alias= --absolute --help --version
12-
*
13-
)
14-
echo "${opts[*]}"
15-
}
16-
17-
_mech_dump() {
1+
_comp_cmd_mech-dump()
2+
{
183
local cur
19-
if declare -F _init_completion >/dev/null 2>&1; then
20-
_init_completion
21-
else
22-
__mech_dump_init_completion
23-
fi
24-
25-
keys=( )
4+
_init_completion
265
cur=${COMP_WORDS[COMP_CWORD]}
276

28-
keys+=( "$(__mech_dump_command_line_options)" )
297
# shellcheck disable=SC2207
30-
COMPREPLY=( $( compgen -W "${keys[*]}" -- "$cur" ) )
31-
return 0
32-
}
8+
COMPREPLY=( $( compgen -W "$(mech-dump --completions)" -- "$cur" ) )
9+
} &&
10+
complete -F _comp_cmd_mech-dump mech-dump
3311

34-
complete -F _mech_dump mech-dump
12+
# ex: filetype=sh

0 commit comments

Comments
 (0)