惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
量子位
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
美团技术团队
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
腾讯CDC
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
博客园 - 【当耐特】
L
LangChain Blog
A
About on SuperTechFans
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
Netflix TechBlog - Medium
博客园_首页
WordPress大学
WordPress大学
博客园 - Franky
Engineering at Meta
Engineering at Meta
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
M
MIT News - Artificial intelligence

WeepingDogel

A Simple Client Server Project Made by Python and Vue3 Annual Summary in 2023 Solve the problem of dual screen with NVIDIA and Intel GPUs How To Transfer A Value From The Parent Component To The Child Component in Vue 3.2 Attempted solution to the OpenStack Provincial Competition problem (Part One: Installation) Attempt to Solve the Problem of VirtualBox Stuck on 'Starting' When Starting a Virtual Machine Some Thoughts on Writing HTML and CSS LAN Penetration Testing with Beef, Bettercap, and Other Tools Help with College Computer Homework
Python Learning Notes - ArgParse
WeepingDogel · 2021-05-16 · via WeepingDogel

Introduction

In order to make TitleGetter more flexible, I plan to let users customize list.txt and the output file. Therefore, this requires the use of command line options… just like some software we commonly use, such as pacman.

/img/2021-05-16-18-31-48屏幕截图.png

So I googled it and learned about ArgParse.

The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv. The argparse module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.

From: argparse â Parser for command-line options, arguments and sub-commands — Python 3.9.5 documentation

Then I tried typing a file…

The result of running it looks like this:

/img/2021-05-16-18-43-35屏幕截图.png

So let’s organize some related notes…

Creating Parser && Adding Options

Before everything starts, we need to use the ArgumentParser usage in the argparse library to create a variable named parser.

1
2
import argparse
parser = argparse.ArgumentParser(description='')

There is a parameter description='' here, which is used for writing some explanations…

For example, we wrote:

1
2
import argparse
parser = argparse.ArgumentParser(description='Welcome')

By the way, we need to write down some necessary options~

parser.add_argument() can be used here.

We need to add some things inside, such as the usage format of options like -a and --about.

Finally, add args = parser.parse_args().

1
2
3
4
import argparse
parser = argparse.ArgumentParser(description='Welcome')
parser.add_argument('-a','--about', help='Show the about')
args = parser.parse_args()

At this point, we can add -h to see the effect.

1
2
3
4
5
6
7
8
usage: a.py [-h] [-a ABOUT]

Welcome

optional arguments:
  -h, --help            show this help message and exit
  -a ABOUT, --about ABOUT
                        Show the about

Then let’s organize a few commonly used parameters.

  1. default * The default value when no parameters are set.
    1
    
    parser.add_argument('-a','--about', help='Show the about', defualt='text.txt')
    
    • If the user does not set this parameter, a default one will be provided automatically.
  2. help
    • Add explanatory text to the corresponding option.
  3. required
    • Used to determine whether this parameter must be set.
    • If required=True is set, an error will be reported if this parameter is not set at runtime.
    1
    
    parser.add_argument('-a','--about', required=True)
    
    1
    2
    3
    
    $ python a.py   
    usage: a.py [-h] -a ABOUT
    a.py: error: the following arguments are required: -a/--about
    

Calling the Obtained Option Parameters

Next, we need to use the obtained parameters.

We know that when something is written after an option on the command line, the program will get it as a string by default. Then we have to use this to do what we want.

I wrote a simple script that can write the contents of one file to another file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import argparse
print('''                                                            
By WeepingDogel
''')
def GetParser():
    parser = argparse.ArgumentParser(description='Help', epilog='A new testing program.')
    parser.add_argument('-o','--output', help='Output file',default='test.txt' , required=True)
    parser.add_argument('-r','--read',help='read a file', required=True)
    return parser
def Write(content, filename):
    f = open(filename,"a")
    f.write(content)
    f.close()
    print(filename + ' has been written')
def Read(filename):
    f = open(filename)
    text = f.read()
    return text
def Main():
    parser = GetParser()
    args = parser.parse_args()
    Write(Read(args.read),args.output)
Main()

It is easy to see that what we obtain will go into the variable args, because it is assigned from the content returned by the function parser.parse_args(). To get the corresponding value of an option parameter, you can access it using args.option_name.

For example, if we want to get the written file name:

1
2
3
4
5
6
import argparse
parser = argparse.ArgumentParser(description='Help', epilog='A new testing program.')
parser.add_argument('-o','--output', help='Output file',default='test.txt' , required=True)
args = parser.parse_args()
filename = args.output
print("The filename is "+ filename)
1
2
$ python b.py -o WeepingDogel
The filename is WeepingDogel

As you can see, we have obtained the string “WeepingDogel”.

Similarly, the file name to be read is the same:

That’s all you need to do ~

Next, let’s take a screenshot of the effect of the above code: /img/2021-05-16-20-05-47屏幕截图.png

Creating and using it is that simple…

Of course, there are more usages to explore…

Conclusion

So what I’m going to do next is to update these into TitleGetter 啦!

There is no need to set the location of list.txt in the configuration file anymore! The output file position does not need to be fixed either!!