How to add more functionality with arguments in this code?
<p>I have found nice little program for making quick notes in terminal. There is a little lack of functionality- I can't read notes I have made with it and also I cannot clear notes from file where they are stored. I would like to modify this program, so i can run it with arguments to read what I have written there and also clean. I have some idea how to do that, but can't find write place in code to paste my lines. I want to run it with arguments such as: -r -c</p>

<p>original program looks like this:</p>

<pre><code>#!/usr/bin/env python3

import time
import os
import sys

# add the current local time to the entry header
lines = [ time.asctime() + '\n' + '--------------------\n' ]

if len( sys.argv ) &gt; 1:
    lines.append( ' '.join( sys.argv[ 1: ] ) )
    lines[-1] += '\n'
else:
    while 1:
        try:
            line = input()
        except EOFError:
            break

        # get more user input until an empty line
        if len( line ) == 0:
            break
        else:
            lines.append( line + '\n' )


# only write the entry if the user entered something
if len( lines ) &gt; 1:
    memoir_path = os.path.expanduser( '~/.memoir' )

    # prepend a seperator only if the file exists ( there are entries already in there )
    if os.path.exists( memoir_path ):
        lines.insert( 0, '--------------------\n' )

    with open( memoir_path, 'a' ) as f:
        f.writelines( lines )
</code></pre>

<p>my code, which I don't know where to paste (if it is correct):</p>

<pre><code># read memoir file
if str(sys.argv) == ("r"):
    os.system('cat ~/.memoir')

# clear memoir file
if str(sys.argv) == ("c"):
    os.system('&gt; ~/.memoir')
</code></pre>

<p>EDIT:</p>

<p>I have made few changes, due to answer, and everything works fine, but I would like to make this code a little simplier. Author of this code added some usles feature for me to run this program with number of random arguments which will be "transformed" into empty lines in note. It seems to not work anyway after my update, so I want to get rid of this feature. I think it starts in line nr 37 look for #here!!! comment</p>

<p>new code looks like this:</p>

<pre><code>#!/usr/bin/env python3

import time
import os
import sys

def help():
    print ("memoir is a minimal cli diary")
    print ("run script with -r argument to read notes")
    print ("run script with -c argument to clear notes file")
    print ("run script with -h argument for help")

# add the current local time to the entry header
lines = [ time.asctime() + '\n' + '------------------------\n' + '\n' ]

if len(sys.argv) &gt;= 2:
    if sys.argv[1] == '-r':
        # read .memoir file
        os.system('cat ~/.memoir')
        print('\n')
        exit(0)

    if sys.argv[1] == '-h':
        # print help
        help()
        exit(0)

    if sys.argv[1] == '-c':
        # clear .memoir file
        os.system('&gt; ~/.memoir')
        exit(0)

    else:
        print("invalid argument, type m -h for help")
        exit(0)

if len(sys.argv) &gt; 1 and len(sys.argv) != 2: #here!!!
    lines.append( ' '.join(sys.argv[ 1: ]))
    lines[-1] += '\n'
else:
    while 1:
        try:
            line = input()
        except EOFError:
            break

        # get more user input until an empty line
        if len( line ) == 0:
            break
        else:
            lines.append( line + '\n' )


# only write the entry if the user entered something
if len( lines ) &gt; 1:
    memoir_path = os.path.expanduser( '~/.memoir' )

    # prepend a seperator only if the file exists ( there are entries already in there )
    if os.path.exists( memoir_path ):
        lines.insert(0, '\n------------------------\n')

    with open( memoir_path, 'a' ) as f:
        f.writelines( lines )

if len(sys.argv) &gt;= 2:
    # clear .memoir file
    if sys.argv[1] == '-c':
        os.system('&gt; ~/.memoir')
</code></pre>

<p>First, <code>sys.argv</code> is an array with the arguments of the command line in it. So you need to test the length of it :</p>

<pre><code>if len(sys.argv) &gt;= 2 :
   #sys.argv[0] is the name of your program
   if sys.argv[1] == '-c' :
</code></pre>

<p>Then you can see that the file is written the latest lines :</p>

<pre><code>if len( lines ) &gt; 1:
memoir_path = os.path.expanduser( '~/.memoir' )

# prepend a seperator only if the file exists ( there are entries already in there )
if os.path.exists( memoir_path ):
    lines.insert( 0, '--------------------\n' )

#here the file is opened with 'a' = append mode. 
#If you want to override the content of the file (for your command 'clear'), use 'w' = write mode.
with open( memoir_path, 'a' ) as f:
    f.writelines( lines )
</code></pre>

<p>So you can include the 'clear' command at the end. The 'read' command will rather find its place at the beggining of the program.</p>

python