Created
April 22, 2018 08:14
-
-
Save jiankaiwang/5de97e0fa5f0b1762ef1ee89adb85df9 to your computer and use it in GitHub Desktop.
basic operations of parameters in CLI over Python
This file contains hidden or 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
| # -*- coding: utf-8 -*- | |
| """ | |
| Created on Sun Apr 22 16:04:40 2018 | |
| @description: Basic Operation in CLI | |
| @author: JianKai Wang | |
| """ | |
| # coding: utf-8 | |
| import sys | |
| import getopt | |
| import logging | |
| # help message | |
| def helpMessgae(): | |
| print("""Usage: python Basic_CLI_Parameters.py [options] | |
| [options] | |
| -o (necessary) | |
| -p (necessary) | |
| -h, --help the help message | |
| """) | |
| # parse opts | |
| def parseOpts(get_opts): | |
| return(dict((k,v) for k,v in get_opts)) | |
| # main entry | |
| logging.basicConfig(level=logging.WARNING) | |
| opts, args = getopt.getopt(sys.argv[1:], "fho:p:", ["help"]) | |
| opts = parseOpts(opts) | |
| if len(opts) < 1: | |
| logging.debug("opts < 1") | |
| helpMessgae() | |
| elif '--help' in opts.keys() or '-h' in opts.keys(): | |
| logging.debug("opts exists help") | |
| helpMessgae() | |
| else: | |
| # the further operations | |
| pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment