| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import argparse |
| 5 |
import os |
| 6 |
|
| 7 |
from .realdir import seedir |
| 8 |
|
| 9 |
helptxt = """ |
| 10 |
Help for the seedir CLI. Function for printing a folder tree |
| 11 |
structure, similar to UNIX `tree`. Directs all arguments to `seedir.seedir()`. |
| 12 |
|
| 13 |
Note that this has been revamped for version v0.3.1. Specifically, parsing |
| 14 |
was moved from getopts to argparse. Options should now all explicitly |
| 15 |
reference parameters of `seedir.seedir()`. |
| 16 |
|
| 17 |
Not all seedir arguments are accepted, namely ones which expect Python |
| 18 |
callables and ones which alter the style. The latter may be added |
| 19 |
in a future version. |
| 20 |
|
| 21 |
Options (short/long): |
| 22 |
|
| 23 |
-b / --beyond BEYOND |
| 24 |
Way to represent items past the depthlimit or itemlimit. |
| 25 |
Options are 'content', 'ellipsis', or explicit string starting |
| 26 |
with an underscore. Not set by default. |
| 27 |
|
| 28 |
-d / --depthlimit DEPTHLIMIT |
| 29 |
Integer limit on depth of folders to enter. Default: None |
| 30 |
|
| 31 |
-f / --first FIRST |
| 32 |
Sort the directory so that either files or folders appear first. |
| 33 |
Options are "folders" or "files". |
| 34 |
|
| 35 |
-h / --help |
| 36 |
Show this message and exit. |
| 37 |
|
| 38 |
-i / --itemlimit ITEMLIMIT |
| 39 |
Integer limit on the number of items to include per directory. |
| 40 |
Default: None |
| 41 |
|
| 42 |
-p / --path PATH |
| 43 |
Dir to see. Default: current directory (os.getcwd()) |
| 44 |
|
| 45 |
-r / --sort_reverse |
| 46 |
Reverse the sort. |
| 47 |
|
| 48 |
-s / --sort |
| 49 |
Sort the contents of each directory by name. |
| 50 |
|
| 51 |
-t / --indent INDENT |
| 52 |
Amount to indent. Default: 2 |
| 53 |
|
| 54 |
-y / --style STYLE |
| 55 |
seedir style to use. Default: "lines" |
| 56 |
|
| 57 |
Options (long only): |
| 58 |
|
| 59 |
--include_folders, --include_files, --exclude_folders, --exclude_files ... |
| 60 |
Folder / file names to include or exclude. Not set by default. Pass |
| 61 |
multiple items as space-separated values. |
| 62 |
|
| 63 |
--regex |
| 64 |
Interpret include/exclude folder/file arguments as Python regex. |
| 65 |
""" |
| 66 |
|
| 67 |
def parse(): |
| 68 |
|
| 69 |
"""Parse command line arguments with argparse.""" |
| 70 |
|
| 71 |
parser = argparse.ArgumentParser(add_help=False) |
| 72 |
|
| 73 |
parser.add_argument('-b', '--beyond', default=None) |
| 74 |
parser.add_argument('-d', '--depthlimit', default=None, type=int) |
| 75 |
parser.add_argument('-f', '--first', default=None) |
| 76 |
parser.add_argument('-h', '--help', action='store_true') |
| 77 |
parser.add_argument('-i', '--itemlimit', default=None, type=int) |
| 78 |
parser.add_argument('-p', '--path', default=os.getcwd()) |
| 79 |
parser.add_argument('-r', '--sort_reverse', action='store_true') |
| 80 |
parser.add_argument('-s', '--sort', action='store_true') |
| 81 |
parser.add_argument('-t', '--indent', default=2, type=int) |
| 82 |
parser.add_argument('-y', '--style', default='lines') |
| 83 |
|
| 84 |
parser.add_argument('--include_folders', default=None, nargs='+') |
| 85 |
parser.add_argument('--exclude_folders', default=None, nargs='+') |
| 86 |
parser.add_argument('--include_files', default=None, nargs='+') |
| 87 |
parser.add_argument('--exclude_files', default=None, nargs='+') |
| 88 |
parser.add_argument('--regex', action='store_true') |
| 89 |
|
| 90 |
return parser.parse_args() |
| 91 |
|
| 92 |
def main(): |
| 93 |
|
| 94 |
"""Parses arguments and passes them to `seedir.seedir()`""" |
| 95 |
|
| 96 |
args = parse() |
| 97 |
if args.help: |
| 98 |
print(helptxt) |
| 99 |
return |
| 100 |
|
| 101 |
kwargs = vars(args) |
| 102 |
del kwargs['help'] |
| 103 |
seedir(**kwargs) |
| 104 |
|
| 105 |
if __name__ == '__main__': |
| 106 |
main() |
| 107 |
|