Debian Codegraph @

examples/synopsis-big

raw
1 #!/usr/bin/perl -w
2
3 # This was originally the example code from Term::ShellUI's documentation
4 # but it grew too big. Now it's intended to demonstrate most of the
5 # available features and still be easy to read.
6
7 # This file is released under the MIT license.
8
9 use strict;
10 use lib '../lib';
11
12
13 use Term::ShellUI;
14 my $term = new Term::ShellUI(
15 commands => get_commands(),
16 history_file => '~/.shellui-synopsis-history',
17 );
18 $term->add_eof_exit_hook(sub {print "Type 'quit' to quit.\n"; return 1;});
19 print 'Using '.$term->{term}->ReadLine."\n";
20 $term->run(@ARGV);
21
22
23 sub get_commands
24 {
25 return {
26 "h" => { alias => "help", exclude_from_completion => 1 },
27 "?" => { alias => "help", exclude_from_completion =>1 },
28 "help" => {
29 desc => "Print helpful information",
30 args => sub { shift->help_args(undef, @_); },
31 method => sub { shift->help_call(undef, @_); }
32 },
33 "history" => { desc => "Prints the command history",
34 doc => "Specify a number to list the last N lines of history\n" .
35 "Pass -c to clear the command history, " .
36 "-d NUM to delete a single item\n",
37 args => "[-c] [-d] [number]",
38 method => sub { shift->history_call(@_) },
39 exclude_from_history => 1,
40 },
41 "exit" => {
42 desc => "Exits the program.",
43 maxargs => 0,
44 method => sub { shift->exit_requested(1); },
45 },
46 "exists" => {
47 desc => "Shows whether files exist",
48 args => sub { shift->complete_files(@_); },
49 proc => sub {
50 print "exists: " .
51 join(", ", map {-e($_) ? "<$_>":$_} @_) .
52 "\n";
53 },
54 doc => <<EOL,
55 Comprehensive documentation for our the exists command.
56 Pass any number of filenames. If a file exists, it is
57 printed in <angle brackets>.
58 This detailed doc can\nspan\nmany\nlines
59 EOL
60 },
61 "show" => {
62 desc => "An example of using subcommands",
63 cmds => {
64 "warranty" => { proc => "You have no warranty!\n" },
65 "args" => {
66 args => [ sub {['create', 'delete']},
67 \&Term::ShellUI::complete_files ],
68 desc => "Print the passed arguments",
69 method => sub {
70 my $self = shift;
71 my $parms = shift;
72 print $self->get_cname($parms->{cname}) .
73 ": " . join(" ",@_), "\n";
74 },
75 },
76 },
77 },
78 "quit" => {
79 desc => "Quit using Fileman",
80 maxargs => 0,
81 method => sub { shift->exit_requested(1); },
82 },
83
84 # Term::ShellUI normally displays "asdf: unknown command".
85 # This shows how to use the default command. If the user
86 # types an unknown command, ShellUI calls '' if it exists.
87 '' => {
88 proc => "No command here by that name!\n",
89 desc => "No help for unknown commands.",
90 doc => "Well, here's a little help: don't type them.\n",
91 },
92 };
93 }
94