Usage of 1.docopt Modules
1 #_*_ coding:utf-8 _*_2 3 4 " "train ticket viewing system on the command line5 Usage:6 index [-GDTKZ] <from> <to> <date>7 8 Options:9 - h,--help Display assistanceTen - g high Speed rail One - D Bullet Train A - T Express - - k Fast - - Z Direct the Example: - index Shanghai Beijing 2018-5-28 - - + - " " + A at - fromDocoptImportdocopt - - defCLI (): -Arguments= docopt (__doc__) - Print(arguments) in - if __name__=='__main__': toCLI ()
Note: Document comments can no longer have a document comment before the type error is reported
Output results
2. Handling command-line arguments
The command-line arguments are stored in the variable SYS.ARGV. The first item in the SYS.ARGV list is always a string that contains the program's file name (' pw.py ') the second item should be the first command-line argument.
Project one: Get the password
Prepare knowledge-----Pyperclip Module
>>> Import Pyperclip
>>> pyperclip.copy (' Hello world! ')
>>> Pyperclip.paste ()
' Hello world! '
Of course, if a program outside your program changes the contents of the Clipboard, the paste () function returns it.
Preparing knowledge-----Command-line arguments
#! Python3
# Pw.py-an insecure password locker program.
passwords = {' email ': ' F7minlbdduvmjuxesskhfhtxftjvb6 ',
' Blog ': ' VMALVQYKAXIVH5G8V01IF1MLZF3SDT ',
' Luggage ': ' 12345 '}
Import Sys
If Len (SYS.ARGV) < 2:
Print (' Usage:python pw.py [account]-Copy account password ')
Sys.exit ()
account = sys.argv[1] # First command line ARG was the account name
If account in passwords:
Pyperclip.copy (Passwords[account])
Print (' Password for ' + account + ' copied to Clipboard. ')
Else
Print (' There is no. named ' + account)
1 #! Python32 3 #Pw.py-an insecure password locker program.4 5passwords = {'Email':'F7minlbdduvmjuxesskhfhtxftjvb6',6 7 'Blog':'VMALVQYKAXIVH5G8V01IF1MLZF3SDT',8 9 'Luggage':'12345'}Ten One ImportSYS A - ifLen (SYS.ARGV) < 2: - the Print('Usage:python pw.py [Account]-Copy account password') - - sys.exit () - +account = Sys.argv[1]#First command line ARG was the account name - + ifAccountinchpasswords: A at pyperclip.copy (Passwords[account]) - - Print('Password for'+ account +'copied to clipboard.') - - Else: - in Print('There is no account named'+ account)
Implementation features: Because the login is the length of the forgotten password, so you can save the device as a. py file, in the form of a dictionary, and then through the combination of the dictionary and the cut version of the password account to achieve the replication of the population.
Extended:
On Windows, you can create a batch file that uses the Win-r run window to run the program (For more information on batch files, see Appendix B). Enter the following code in the file editor, save as Pw.bat, and place it in the C:\Windows directory:
@py. exe C:\Python34\pw.py%*
@pause
with this batch file, run the Password saver on Windows, just press win-r, and then enter PW.
Python document comment parameter get