I am using argparse library to use different arguments in my script. I am passing the output of the below result to the result.txt file.
I have a table name test_arguments where i need to store different argument name and description. example from below i need to insert :
Insert into table test_argument ( arg_name, arg_desc ) as ( num, The fibnocacci number to calculate:);
Insert into table test_argument ( arg_name, arg_desc ) as ( help, show this help message and exit) ;
Insert into table test_argument ( arg_name, arg_desc ) as ( file, output to the text file) ;
How can i read this file and extract these two fields from the below 'result.txt' file? Which is the best way to do it?
python sample.py -h >> result.txt
result.txt
-----------
usage: sample.py [-h] [-f] num
To the find the fibonacci number of the give number
positional arguments:
num The fibnocacci number to calculate:
optional arguments:
-h, --help show this help message and exit
-f, --file Output to the text file
Updated : My code
import re
list = []
hand = open('result.txt','r+')
for line in hand:
line = line.rstrip()
if re.search('positional', line) :
line = hand.readline()
print(line)
elif re.search('--',line):
list.append(line.strip())
print(list)
Output :
num The fibnocacci number to calculate:
['-h, --help show this help message and exit', '-f, --file Output to the text file']
I am not trying to extract (file,Output to the text file) and (help,show this help message and exit) from this list but finding it difficult to parse them. Any inputs on this?
parserobject. Here do you only have access to thehelpmessage?