-
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.
Adding examples and tuning interfaces.
- Loading branch information
Showing
8 changed files
with
174 additions
and
34 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,34 @@ | ||
""" | ||
A Frankenstein combo of advanced style and simple (autocommand). | ||
""" | ||
|
||
import shellish | ||
|
||
|
||
@shellish.autocommand | ||
def subcommand1(firstarg, second, *args, optional=1, optionalstr:str=None, | ||
mustbeint:int=None, **kwargs:bool): | ||
print("Hi from sub1", firstarg, second, args, optional, optionalstr, | ||
mustbeint, kwargs) | ||
|
||
|
||
@shellish.autocommand | ||
def subcommand2(firstarg, second, *args, optional=1, optionalstr:str=None, | ||
mustbeint:int=None, **kwargs:bool): | ||
print("Hi from sub2", firstarg, second, args, optional, optionalstr, | ||
mustbeint, kwargs) | ||
|
||
|
||
class Root(shellish.Command): | ||
""" Shellify some autocommands. """ | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.add_subcommand(subcommand1) | ||
self.add_subcommand(subcommand2) | ||
|
||
def run(self, args): | ||
self.shell() | ||
|
||
root = Root() | ||
root() |
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,19 @@ | ||
""" | ||
Nesting commands using simple syntax. | ||
""" | ||
|
||
import shellish | ||
|
||
|
||
@shellish.autocommand | ||
def main(): | ||
print("Default Action") | ||
|
||
|
||
@shellish.autocommand | ||
def sub(option=None): | ||
print("Hi from sub", option) | ||
|
||
|
||
main.add_subcommand(sub) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
Demo of different ways to skin a cat. | ||
""" | ||
|
||
import shellish | ||
|
||
############## | ||
# Decorators # | ||
############## | ||
@shellish.autocommand | ||
def cat1(): | ||
cat1.shell() | ||
|
||
@shellish.autocommand | ||
def sub1(optional:int=1): | ||
print("ran subcommand1", optional) | ||
|
||
cat1.add_subcommand(sub1) | ||
|
||
|
||
############### | ||
# Composition # | ||
############### | ||
cat2 = shellish.Command(name='cat2', doc='composition cat') | ||
cat2.run = lambda args: cat2.shell() | ||
|
||
sub2 = shellish.Command(name='sub2', doc='composition cat sub') | ||
sub2.add_argument('--optional', type=int, default=2) | ||
sub2.run = lambda args: print("ran subcommand2", args.optional) | ||
cat2.add_subcommand(sub2) | ||
|
||
|
||
############### | ||
# Inheritance # | ||
############### | ||
class Cat3(shellish.Command): | ||
""" Inheritance cat. """ | ||
|
||
name = 'cat3' | ||
|
||
def run(self, args): | ||
self.shell() | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.add_subcommand(Sub3) | ||
|
||
|
||
class Sub3(shellish.Command): | ||
""" Inheritance cat sub. """ | ||
|
||
name = 'sub3' | ||
|
||
def setup_args(self, parser): | ||
self.add_argument('--optional', type=int, default=3) | ||
|
||
def run(self, args): | ||
print("ran subcommand3", args.optional) | ||
|
||
|
||
# Putting it together for a demo.. | ||
main = shellish.Command(name='main', doc='harness') | ||
main.add_subcommand(cat1) | ||
main.add_subcommand(cat2) | ||
main.add_subcommand(Cat3) | ||
main.shell() |
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,18 @@ | ||
""" | ||
Demo how to add your auto tab completers. | ||
""" | ||
|
||
import random | ||
import shellish | ||
|
||
def thing_completer(prefix): | ||
letters = 'qwertyuiopasdfghjklzxcvbnm' | ||
word = lambda: ''.join(random.sample(letters, random.randint(1, 16))) | ||
return set(word() for x in range(random.randint(1, 1000))) | ||
|
||
thing = shellish.Command(name='thing', doc='Demo Tab Completion') | ||
thing.add_argument('--choices', choices=['one', 'two', 'three']) | ||
thing.add_argument('--function', complete=thing_completer) | ||
root = shellish.Command() | ||
root.add_subcommand(thing) | ||
root.shell() |
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