-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add script for auto complete shell script generation
which except not only scripts but also directories
- Loading branch information
1 parent
5964438
commit e988122
Showing
5 changed files
with
73 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import argparse | ||
import glob | ||
import os | ||
import sys | ||
from typing import List | ||
|
||
import argser | ||
|
||
argcomplete_desc = "Add auto completion for scripts. Ex: eval \"$(python -m argser auto foo.py)\"" | ||
|
||
|
||
class AutoArgs: | ||
executables: List[str] = argser.PosArg() | ||
use_defaults = True | ||
complete_arguments = argser.Arg(nargs=argparse.REMAINDER) | ||
shell: str = argser.Arg(choices=('bash', 'tcsh', 'fish'), default='bash') | ||
|
||
|
||
def autocomplete(args: AutoArgs): | ||
import argcomplete | ||
|
||
exs = [] | ||
for i, ex in enumerate(args.executables): | ||
if os.path.isdir(ex): | ||
exs.extend(glob.glob(os.path.join(ex, '*.py'))) | ||
else: | ||
exs.append(ex) | ||
# noinspection PyTypeChecker | ||
sys.stdout.write(argcomplete.shellcode(exs, args.use_defaults, args.shell, args.complete_arguments)) | ||
|
||
|
||
class Args: | ||
auto = argser.sub_command(AutoArgs, help=argcomplete_desc, description=argcomplete_desc) | ||
|
||
|
||
def main(): | ||
args = argser.parse_args(Args, parser_prog='argser') | ||
if args.auto: | ||
autocomplete(args.auto) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters