5

I want to make a script in python and then run it from the command line. The script is called test.py and the command to run it is:

python3 test.py John Jackson

Here is the expected output:

John Jackson

And here is the python script I made:

class person:

    def __init__(self, first, last):
        self.firstname = first
        self.lastname = last

    def get_first(self):
        return self.firstname

    def get_last(self):
        return self.lastname

import argparse

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('first')
    parser.add_argument('last')
    args = parser.parse_args()
    c1 = person(args.first, args.last)
    print(c1)

But the problem is when I run the script from the command line using the mentioned command, it returns nothing. Do you know how to fix it and get the expected output?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
John
  • 131
  • 1
  • 2
  • 10

3 Answers3

5

You defined your main() function but did not call it.

Add this at the end of your script:

if __name__ == "__main__":
    main()

See What does if __name__ == “__main__”: do?.

Then, c1 is the person instance, which would print:

$ python3 test.py John Jackson
<__main__.person object at 0x104907ef0>

You need to call get_first() and get_last() to get the correct output:

print(c1.get_first(), c1.get_last())
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
2

I am not sure if you are calling your main function while running the script.

Add

if __name__ == "__main__":
    main()

And override __str__ function of the person class to print first name and last name while passing class as argument to print.

def __str__(self):
    return self.firstname + ' ' + self.lastname

The whole code goes like this

import argparse

class person:

    def __init__(self, first, last):
        self.firstname = first
        self.lastname = last

    def get_first(self):
        return self.firstname

    def get_last(self):
        return self.lastname

    def __str__(self):
        return self.firstname + ' ' + self.lastname

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('first')
    parser.add_argument('last')
    args = parser.parse_args()
    c1 = person(args.first, args.last)
    print(c1)

if __name__ == '__main__':
    main()
Akhil Batra
  • 581
  • 4
  • 16
0

Your script is unnecessarily complex, a much (much) simpler way to achieve this is to use the argv property of the sys module then separate the (string) argument using split, like this:

import sys

name = sys.argv[1].split(" ")

if len(name) != 2:
  raise ValueError("Invalid first and last name, try using \"John Jackson\" instead.")

[first_name, last_name] = name
print(first_name, last_name)

You don't even need to raise an error:

import sys

name = sys.argv[1].split(" ")

if len(name) == 2:
  [first_name, last_name] = name
  print(first_name, last_name)

Then when you run the script like this:

python3 filename.py "John Jackson"

You should get the following output:

John Jackson

Good luck.

Malekai
  • 4,765
  • 5
  • 25
  • 60