Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

Commit a1bace7

Browse files
committed
Merge pull request #5 from marksteve/cli
Add cli script
2 parents b8329e2 + 5228f0f commit a1bace7

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

kaptan/__init__.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,40 @@ def export(self, handler=None):
8585
handler_class = self.HANDLER_MAP[handler]()
8686

8787
return handler_class.dump(self.configuration_data)
88+
89+
90+
def main():
91+
import argparse
92+
import os
93+
parser = argparse.ArgumentParser(
94+
description='Configuration manager in your pocket')
95+
parser.add_argument('config_file', action='store',
96+
help="file to load config from")
97+
parser.add_argument('--handler', action='store',
98+
help="handler to use (default: guessed from filename)")
99+
parser.add_argument('-e', '--export', action='store', default='json',
100+
help="format to export to")
101+
parser.add_argument('-k', '--key', action='store',
102+
help="config key to get value of")
103+
args = parser.parse_args()
104+
HANDLER_EXT = {
105+
'ini': 'ini',
106+
'conf': 'ini',
107+
'yaml': 'yaml',
108+
'json': 'json',
109+
'py': 'file',
110+
}
111+
handler = (args.handler or
112+
HANDLER_EXT.get(
113+
os.path.splitext(args.config_file)[1][1:],
114+
None
115+
))
116+
if not handler:
117+
raise RuntimeError("Unable to determine handler")
118+
with open(args.config_file) as f:
119+
config = Kaptan(handler=handler)
120+
config.import_config(f.read())
121+
if args.key:
122+
print config.get(args.key)
123+
else:
124+
print config.export(args.export)

setup.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from distutils.core import setup
1+
from setuptools import setup
22

33
setup(
44
name='kaptan',
@@ -9,5 +9,10 @@
99
author='Emre Yilmaz',
1010
author_email='mail@emreyilmaz.me',
1111
description='Configuration Manager',
12-
requires=['PyYAML', ],
12+
install_requires=['PyYAML', ],
13+
entry_points=dict(
14+
console_scripts=[
15+
'kaptan = kaptan:main',
16+
],
17+
),
1318
)

0 commit comments

Comments
 (0)