Last active
December 14, 2015 18:49
-
-
Save akuwano/5132283 to your computer and use it in GitHub Desktop.
It is Python Command Line Template.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import argparse | |
import traceback | |
import logging | |
class ArgumentParse(object): | |
""" | |
Parse Argument. | |
""" | |
def __init__(self): | |
try: | |
parser = argparse.ArgumentParser(description='Command Description') | |
parser.add_argument('-f', action="store", dest="file", \ | |
help=u"読み込みファイル defualt=testfile", \ | |
default="testfile") | |
self.optargs = parser.parse_args() | |
self.checkopts() | |
except Exception, e: | |
print >>sys.stderr, "Argument Parse Error" | |
print >>sys.stderr, str(e) | |
sys.exit(1) | |
def checkopts(self): | |
if not self.optargs.file: | |
self.opt_messages.append("-f options is required.") | |
sys.exit(1) | |
class Command(object): | |
""" | |
Parent Command Class. | |
""" | |
def __init__(self, argparse): | |
self.optargs = argparse | |
def run(self): | |
pass | |
class TestCommand(Command): | |
""" | |
Test COmmand Class. | |
""" | |
def run(self): | |
print self.optargs.file | |
def main(): | |
arguparse = ArgumentParse() | |
command = TestCommand(arguparse.optargs) | |
command.run() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment