| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import argparse |
| 4 |
|
| 5 |
from crazy_complete import argparse_mod |
| 6 |
|
| 7 |
argp = argparse.ArgumentParser(prog='crazy-complete-test', |
| 8 |
description='Test argument parser for shell completion') |
| 9 |
|
| 10 |
argp.add_argument('--version', action='version') |
| 11 |
|
| 12 |
# ============================================================================= |
| 13 |
# SUBCOMMANDS |
| 14 |
# ============================================================================= |
| 15 |
subp = argp.add_subparsers(description='commands') |
| 16 |
|
| 17 |
# ============================================================================= |
| 18 |
# Command 'complete' |
| 19 |
# ============================================================================= |
| 20 |
cmdp = subp.add_parser('complete', help='Test complete commands') |
| 21 |
|
| 22 |
cmdp.add_argument('--exec', help='Parse output').complete('exec', 'printf "%s\\t%s\\n" "Item 1" "Description 1" "Item 2" "Description 2"') |
| 23 |
cmdp.add_argument('--exec-fast', help='Parse output (fast)').complete('exec_fast', 'printf "%s\\t%s\\n" "Item_1" "Description 1" "Item_2" "Description 2"') |
| 24 |
cmdp.add_argument('--exec-internal', help='Execute internal code').complete('exec_internal', '_complete_internal') |
| 25 |
cmdp.add_argument('--exec-large', help='Parse output (large date)').complete('exec', 'seq 1 99999') |
| 26 |
cmdp.add_argument('--file', help='Complete a file').complete('file') |
| 27 |
cmdp.add_argument('--directory', help='Complete a directory').complete('directory') |
| 28 |
cmdp.add_argument('--file-tmp', help='Complete a file in /tmp').complete('file', {'directory': '/tmp'}) |
| 29 |
cmdp.add_argument('--directory-tmp', help='Complete a directory in /tmp').complete('directory', {'directory': '/tmp'}) |
| 30 |
cmdp.add_argument('--user', help='Complete a user').complete('user') |
| 31 |
cmdp.add_argument('--group', help='Complete a group').complete('group') |
| 32 |
cmdp.add_argument('--uid', help='Complete a uid').complete('uid') |
| 33 |
cmdp.add_argument('--gid', help='Complete a gid').complete('gid') |
| 34 |
cmdp.add_argument('--command', help='Complete a command').complete('command') |
| 35 |
cmdp.add_argument('--process', help='Complete a process').complete('process') |
| 36 |
cmdp.add_argument('--pid', help='Complete a pid').complete('pid') |
| 37 |
cmdp.add_argument('--signal', help='Complete a signal').complete('signal') |
| 38 |
cmdp.add_argument('--hostname', help='Complete a hostname').complete('hostname') |
| 39 |
cmdp.add_argument('--variable', help='Complete a variable').complete('variable') |
| 40 |
cmdp.add_argument('--environment', help='Complete a enviroment variable').complete('environment') |
| 41 |
cmdp.add_argument('--service', help='Complete a service').complete('service') |
| 42 |
cmdp.add_argument('--choices', help='Complete from choices', choices={'foo:bar': 'Description for foo:bar', 1:'one', 2:'two'}) |
| 43 |
cmdp.add_argument('--value-list', help='Complete a list').complete('value_list', {'values': ['foo', 'bar', 'baz']}) |
| 44 |
cmdp.add_argument('--value-list-2', help='Complete a list').complete('value_list', {'values': {'foo': 'Some foo', 'bar': 'Some bar'}}) |
| 45 |
cmdp.add_argument('--value-list-dup', help='Complete a list (with duplicates)').complete('value_list', {'values': ['foo', 'bar', 'baz'], 'duplicates': True}) |
| 46 |
cmdp.add_argument('--value-list-colon', help='Complete a list (colon)').complete('value_list', {'values': ['foo', 'bar', 'baz'], 'separator': ':'}) |
| 47 |
cmdp.add_argument('--range-1', help='Complete a range', type=int, choices=range(1,10)) |
| 48 |
cmdp.add_argument('--range-2', help='Complete a range', type=int, choices=range(1,10,2)) |
| 49 |
cmdp.add_argument('--history', help='Complete a history').complete('history', '[^"\' ]+@[^"\' ]+(/[^"\' ]+)?') |
| 50 |
cmdp.add_argument('--commandline', help='Complete a command line').complete('commandline_string') |
| 51 |
cmdp.add_argument('--mountpoint', help='Complete a mountpoint').complete('mountpoint') |
| 52 |
cmdp.add_argument('--net', help='Complete a network interface').complete('net_interface') |
| 53 |
cmdp.add_argument('--shell', help='Complete a login shell').complete('login_shell') |
| 54 |
cmdp.add_argument('--locale', help='Complete a locale').complete('locale') |
| 55 |
cmdp.add_argument('--charset', help='Complete a charset').complete('charset') |
| 56 |
cmdp.add_argument('--timezone', help='Complete a timezone').complete('timezone') |
| 57 |
cmdp.add_argument('--alsa-card', help='Complete an ALSA card').complete('alsa_card') |
| 58 |
cmdp.add_argument('--alsa-device', help='Complete an ALSA device').complete('alsa_device') |
| 59 |
cmdp.add_argument('--mime-image', help='Complete an image').complete('mime_file', 'image/') |
| 60 |
cmdp.add_argument('--mime-video', help='Complete a video').complete('mime_file', 'video/') |
| 61 |
cmdp.add_argument('--date', help='Complete a date').complete('date', '%Y-%m-%d') |
| 62 |
cmdp.add_argument('--date-format', help='Complete a date format').complete('date_format') |
| 63 |
cmdp.add_argument('--file-list', help='Complete a list of files').complete('file_list') |
| 64 |
cmdp.add_argument('--directory-list', help='Complete a list of directories').complete('directory_list') |
| 65 |
cmdp.add_argument('--filesystem-type', help='Complete a filesystem type').complete('filesystem_type') |
| 66 |
cmdp.add_argument('--prefix', help='Complete with a prefix').complete('prefix', 'prefix:', ['file']) |
| 67 |
cmdp.add_argument('--prefix-list', help='Complete with a prefix').complete('prefix', 'prefix:', ['file_list']) |
| 68 |
cmdp.add_argument('--ip', help='Complete an ip address').complete('ip_address', 'all') |
| 69 |
cmdp.add_argument('--ip-all', help='Complete an ip address').complete('ip_address', 'all+') |
| 70 |
cmdp.add_argument('--ip-v4', help='Complete an ipv4 address').complete('ip_address', 'ipv4') |
| 71 |
cmdp.add_argument('--ip-v6', help='Complete an ipv6 address').complete('ip_address', 'ipv6') |
| 72 |
cmdp.add_argument('--ip-v4-all', help='Complete an ipv4 address').complete('ip_address', 'ipv4+') |
| 73 |
cmdp.add_argument('--ip-v6-all', help='Complete an ipv6 address').complete('ip_address', 'ipv6+') |
| 74 |
cmdp.add_argument('--integer', help='Complete an integer').complete('integer', {'suffixes': {'s': 'seconds', 'm': 'minutes', 'h': 'hours'}}) |
| 75 |
cmdp.add_argument('--float', help='Complete a float').complete('float', {'suffixes': {'s': 'seconds', 'm': 'minutes', 'h': 'hours'}}) |
| 76 |
|
| 77 |
# ============================================================================= |
| 78 |
# Command 'when' |
| 79 |
# ============================================================================= |
| 80 |
cmdp = subp.add_parser('when', help='Test the "when"-feature') |
| 81 |
|
| 82 |
cmdp.add_argument('--var', '-var', '-V', help='Conditional variable') |
| 83 |
cmdp.add_argument('--optional', '-optional', '-O', help='Conditional variable', nargs='?') |
| 84 |
cmdp.add_argument('--if-var', help='Only show option if --var is given').when('has_option --var -var -V') |
| 85 |
cmdp.add_argument('--if-var-is-foo', help='Only show option if --var is foo or bar').when('option_is --var -var -V -- foo bar') |
| 86 |
cmdp.add_argument('--if-optional', help='Only show option if --optional is given').when('has_option --optional -optional -O') |
| 87 |
cmdp.add_argument('--if-optional-is-foo', help='Only show option if --optional is foo').when('option_is --optional -optional -O -- foo') |
| 88 |
|
| 89 |
#cmdp.add_argument('--if-var-or-optional-is-foo').when('option_is --var --optional -- foo') |
| 90 |
#cmdp.add_argument('--if-not-var', help='Only show option if --var is not given').when('not_has_option --var -v') |
| 91 |
|
| 92 |
# ============================================================================= |
| 93 |
# Command 'argparse-actions' |
| 94 |
# ============================================================================= |
| 95 |
cmdp = subp.add_parser('argparse-actions', help='argparse tool actions') |
| 96 |
|
| 97 |
cmdp.add_argument('--store-true', help='A option flag', action='store_true') |
| 98 |
cmdp.add_argument('--store-false', help='A option flag', action='store_false') |
| 99 |
cmdp.add_argument('--store-const', help='A option flag', action='store_const', const='bar') |
| 100 |
cmdp.add_argument('--append-const', help='A option flag', action='append_const', const='bar') |
| 101 |
cmdp.add_argument('--append', help='A option flag', action='append') |
| 102 |
cmdp.add_argument('--count', help='A option flag', action='count') |
| 103 |
cmdp.add_argument('--extend', help='A option flag', action='extend') |
| 104 |
|
| 105 |
# ============================================================================= |
| 106 |
# Command 'subcommand' |
| 107 |
# ============================================================================= |
| 108 |
cmdp = subp.add_parser('subcommand', help='Test nested subcommands') |
| 109 |
|
| 110 |
cmdp.add_argument('--subcommand-choices', help='Complete from choices', choices=(1,'two and a half',3)) |
| 111 |
|
| 112 |
subp1 = cmdp.add_subparsers(description='commands') |
| 113 |
|
| 114 |
cmdp1 = subp1.add_parser('sub-subcommand', help='Nested subcommand') |
| 115 |
|
| 116 |
cmdp1.add_argument('--sub-subcommand-choices', help='Complete from choices', choices=(1,'two and a half',3)) |
| 117 |
|
| 118 |
# ============================================================================= |
| 119 |
# Command 'test' |
| 120 |
# ============================================================================= |
| 121 |
cmdp = subp.add_parser('test', help='For testing the completer').aliases(['alias1', 'alias2']) |
| 122 |
|
| 123 |
cmdp.add_argument('-F', '-flag', '--flag', help='A option flag', action='store_true') |
| 124 |
cmdp.add_argument('-O', '-optional', '--optional', help='Option with optional arg', nargs='?', choices=(1,2,3)) |
| 125 |
cmdp.add_argument('-A', '-arg', '--arg', help='Option with arg', choices=(1,2,3)) |
| 126 |
cmdp.add_argument('--special-chars-in-description', help='Here are some special chars: $"\'\\[]*`)') |
| 127 |
cmdp.add_argument('--repeatable-flag', action='store_true').set_repeatable() |
| 128 |
cmdp.add_argument('--repeatable-arg').set_repeatable() |
| 129 |
cmdp.add_argument('-H', '-hidden', '--hidden', help=argparse.SUPPRESS, choices=[1,2,3]) |
| 130 |
cmdp.add_argument('--hidden-flag', help=argparse.SUPPRESS, action='store_true') |
| 131 |
|
| 132 |
group = cmdp.add_mutually_exclusive_group() |
| 133 |
group.add_argument('--exclusive-1', action='store_true') |
| 134 |
group.add_argument('--exclusive-2', action='store_true') |
| 135 |
|
| 136 |
cmdp.add_argument('positional_1', help='First positional', choices=('first1', 'first2', 'first3')) |
| 137 |
cmdp.add_argument('positional_2', help='Second positional', choices=('second1', 'second2')) |
| 138 |
cmdp.add_argument('positional_3', help='Repeated positional', choices=('repeated1', 'repeated2'), nargs='+') |
| 139 |
|
| 140 |
if __name__ == '__main__': |
| 141 |
argp.parse_args() |