Saturday, September 18, 2021

Python: Easy help print

As a follow up of the "Bash: Easy help print", lets do the same thing. So I will just copy the whole text and replace what needs replacing...

Ok, lets create a single line of python that will handle the -h option and print something.

First, what to print? I usually add some comments on the start of the file, so lets print that. Example script:

#!/bin/env python3
#
#  This is the doc of the script
#

the line that we will add

code of script

Lets go step by step. We need to print the file it self:

h = open(args[0], 'r').read()
print(h)

Skip the first line (lets skip the read):

print(h[h.index('\n')+1:])	

Go until you find the first empty line:

print(h[h.index('\n')+1:h.index('\n\n')]

Remove the # fro the start of the line:

.replace('#','')

Finally, we check if the -h is the first argument, print the thing and exit:

if '-h' in args: h = open(args[0], 'r').read(); print(h[h.index('\n')+1:h.index('\n\n')].replace('#','')); exit()



done_