0

I am trying to change some variables, based on program arguments.

This is my code:

#initialize all scaling factors to 1
x_scale = 1
y_scale = 1
z_scale = 1

#the second argument is the axis to scale (1-3)
axis_Select = (sys.argv[2])
#the third argument is the scaling factor
scaling_Factor = (sys.argv[3])

if axis_Select == 1:
    x_scale = scaling_Factor
elif axis_Select == 2:
    y_scale = scaling_Factor
elif axis_Select == 3:
    z_scale = scaling_Factor

Then I invoke the program with:

python3 testScript.py /home/user/Desktop/file.extenstion 2 2

Then if i do a print(y_scale), it is 1, instead of 2 that it should be.

This works for other combinations as well, I just used the y_scale as an example.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
user1584421
  • 3,499
  • 11
  • 46
  • 86

2 Answers2

3

The elements of sys.argv are strings, you need to convert them to integers before comparing with integers.

#the second argument is the axis to scale (1-3)
axis_Select = int(sys.argv[2])
#the third argument is the scaling factor
scaling_Factor = int(sys.argv[3])
Barmar
  • 741,623
  • 53
  • 500
  • 612
2

You can use argparse for this

import argparse

parser = argparse.ArgumentParser()

parser.add_argument(
        "path", type=str, help="file path")
parser.add_argument(
        "axis_select",
        type=int)
parser.add_argument(
        "scaling_factor",
        type=int)

options = parser.parse_args()

options will contain provided values for path, axis_select, scaling_factor accessible by options.axis_select etc

note that this will require 3 positional arguments - path, axis_select, scaling_factor This will also convert it to proper type at the time

if you need to make those optional you can use

parser.add_argument(
    "--path",
    default="/somedefaultpath",
)

In this case you'll need to specify it with --path /something

More details https://docs.python.org/3/library/argparse.html

ihoryam
  • 1,941
  • 2
  • 21
  • 24