diff --git a/contrib/README.md b/contrib/README.md new file mode 100644 index 00000000..b3152ca2 --- /dev/null +++ b/contrib/README.md @@ -0,0 +1,13 @@ +# Other Content + +## Command Completion + +### Installation + +For Bash: + +Copy the completion script to operating system specific folder, e.g. + +* /usr/share/bash-completion/completions/ +* /etc/bash_completion.d + diff --git a/contrib/bash-completion/completions/mech-dump b/contrib/bash-completion/completions/mech-dump new file mode 100644 index 00000000..bf5e7a78 --- /dev/null +++ b/contrib/bash-completion/completions/mech-dump @@ -0,0 +1,11 @@ +_comp_cmd_mech_dump() +{ + local cur + cur=${COMP_WORDS[COMP_CWORD]} + + # shellcheck disable=SC2207,SC2016 + COMPREPLY=( $( compgen -W '$(mech-dump --completions)' -- "$cur" ) ) +} && + complete -F _comp_cmd_mech_dump mech-dump + +# ex: filetype=sh diff --git a/script/mech-dump b/script/mech-dump index 2df1e11b..0e423d09 100755 --- a/script/mech-dump +++ b/script/mech-dump @@ -20,7 +20,7 @@ my $agent; my $agent_alias; my $cookie_filename; -GetOptions( +my %command_line_options = ( 'user=s' => \$user, 'password=s' => \$pass, headers => sub { push( @actions, \&dump_headers ) }, @@ -30,18 +30,39 @@ GetOptions( all => sub { $all++; push( - @actions, \&dump_headers, \&dump_forms, \&dump_links, - \&dump_images + @actions, + \&dump_headers, \&dump_forms, \&dump_links, \&dump_images ); }, text => sub { push( @actions, \&dump_text ) }, - absolute => \$absolute, + 'absolute!' => \$absolute, 'agent=s' => \$agent, 'agent-alias=s' => \$agent_alias, 'cookie-file=s' => \$cookie_filename, help => sub { pod2usage(1); }, version => sub { print STDERR $WWW::Mechanize::VERSION, "\n"; exit 0; }, -) or pod2usage(2); + 'completions' => sub { completions(@ARGV); }, +); +GetOptions(%command_line_options) or pod2usage(2); + +sub completions { + my (@words) = @_; + my @opts; + foreach ( sort keys %command_line_options ) { + if (m/^ (? [^!]+) ! $/msx) { + push @opts, $+{opt}, 'no-' . $+{opt}; + } + elsif (m/^ (? [^=]+) = [siof]{1} $/msx) { + push @opts, ( split qr{\|}, $+{opt} ); + } + else { + push @opts, $_; + } + } + print join "\n", ( map { q{--} . $_ } @opts ); + print "\n"; + exit 0; +} =head1 SYNOPSIS @@ -103,7 +124,8 @@ elsif ( defined $agent_alias ) { } if ( defined $cookie_filename ) { my $cookies = HTTP::Cookies->new( - file => $cookie_filename, autosave => 1, + file => $cookie_filename, + autosave => 1, ignore_discard => 1 ); $cookies->load();