Skip to content

Commit 7658ed3

Browse files
committed
Initial version
0 parents  commit 7658ed3

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
BSD 2-Clause License
2+
3+
Copyright (c) 2018, darkclouder
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# cli-context
2+
This is a library which supports CLI contexts for action hierarchies.
3+
For that, you have to use the `CliContext` class and create your own contexts.

cli-context/__init__.py

Whitespace-only changes.

cli-context/cli_context.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class CliContext:
2+
def __init__(self, description, action=None, sub_contexts=None):
3+
self.description = description
4+
self.action = action
5+
6+
if self.action is None:
7+
self.sub_contexts = sub_contexts
8+
9+
def run(self, program, args):
10+
if self.action is not None:
11+
return self.action(program, args)
12+
else:
13+
if len(args) == 0:
14+
self.print_help(program)
15+
else:
16+
cmd, sub_args = args[0], args[1:]
17+
18+
if cmd == "-h" or cmd == "--help":
19+
self.print_help(program)
20+
elif cmd in self.sub_contexts:
21+
if self.sub_contexts[cmd].run(program + (cmd,), sub_args):
22+
self.print_help(program)
23+
else:
24+
print("Invalid command %s" % cmd)
25+
self.print_help(program)
26+
27+
def print_help(self, program):
28+
if self.sub_contexts is not None:
29+
program_str = " ".join(program)
30+
31+
print("usage: %s <command> [<args>]" % program_str)
32+
33+
if len(self.sub_contexts) > 0:
34+
print("\nCommands:")
35+
for (cmd, context) in self.sub_contexts.items():
36+
print("%s - %s" % (cmd, context.description))
37+
38+
print(
39+
"\nFor detailed help of each command enter {0} <command> --help,\ne.g. {0} {1} --help".format(
40+
program_str, next(iter(self.sub_contexts))
41+
)
42+
)

setup.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from setuptools import setup, find_packages
2+
3+
4+
setup(
5+
name="cli-context",
6+
version="0.1.0",
7+
description="Hierarchical CLI creator",
8+
long_description="".join([
9+
"Library which supports CLI contexts for action hierarchies",
10+
]),
11+
url="https://github.com/darkclouder/cli-context",
12+
author="darkclouder",
13+
classifiers=[
14+
"Development Status :: 4 - Beta",
15+
"Intended Audience :: Developers",
16+
"License :: OSI Approved :: BSD License",
17+
"Programming Language :: Python :: 3.6",
18+
"Topic :: Software Development :: Libraries",
19+
],
20+
packages=find_packages(exclude=["contrib", "docs", "tests*"]),
21+
)

0 commit comments

Comments
 (0)