entering arguments at the command-line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Deathwing
    New Member
    • Mar 2007
    • 32

    entering arguments at the command-line

    Helll I am trying to figure out how to enter arguments throught he command line for a program I have created that is already working. I just want to adapt it to work at the command line. Here is the code.

    Code:
    import random
    
    print\
           """
                                #---------------#
                                #    RPG 1.0    #
                                #---------------#
                                
    Welcome to Random_Point_Generator 1.0 (RPG 1.0),RPG is very simple to use.
    RPG generates a series of random points and writes these points to a .xyz
    file.  In order to generate these points RPG asks the user to input two sets
    of coordinates and the number of points the user wishs to generate.RPG first
    asks for an upper_left and a lower_right set of coordinates.It will then ask
    the user for the number of points that the they wishs it to generate.
    Random points will be generated within the bounds of the upper_left and
    lower_right coordinate values.The program terminates when the user has
    finished entering their xLR and yLR values.
          """
    
    
    
    print "\nPlease enter the upper left coordinates\n"
    xUL = float(raw_input("enter your x value: "))
    yUL = float(raw_input("enter you y value: "))
    
    print "\nPlease enter the lower right coordinates\n"
    xLR = float(raw_input("enter your x value: "))
    yLR = float(raw_input("enter your y value: "))
    
    
    points = int(raw_input("\n\nHow many points do you want generated? "))
    
    f = open(r'points.xyz', 'w')
    
    
    f.writelines(['%9.2f%14.2f\n' % (random.uniform(xUL,xLR),random.uniform(yLR,yUL))for _ in range(points)])
    
    
    f.close()
    
    print '\nThanks for using RPG 1.0, your "points.xyz" file should now have been created.'
    
    raw_input("\nPress Enter to exit.")
    I would like to make it so that the user is never asked to enter anything just so that they can enter arguments from the c:\ for example the entering of carguments would look like this

    c:\2535358.68,2 3689.36,1423.35 ,8236.32,2000,p oints.txt

    basically in the above there are six arguments entered, X and Y coordinates for the upper left value, X and Y coordinates for the lower right value, the 2000 is for the amount of pionts the user wishs to generate and the points.txt is the file they wisht to be created where these points will be stored? I have looked at some command line parsing but it's rather technical. Any help would be great thanks.
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    what you need is the sys module. example
    Code:
    import sys
    arguments =  sys.argv[1:]  #get all arguments
    ...
    ..

    Comment

    • Deathwing
      New Member
      • Mar 2007
      • 32

      #3
      Originally posted by ghostdog74
      what you need is the sys module. example
      Code:
      import sys
      arguments =  sys.argv[1:]  #get all arguments
      ...
      ..

      Thanks I should have been more specific, I have looked at the sys module, but how do I specify the arguments and how do I then run the program at the command line ?

      Comment

      • dshimer
        Recognized Expert New Member
        • Dec 2006
        • 136

        #4
        I just use
        Code:
        python progname.py arg1 arg2
        remember that argv[0] is the program name itself, for example
        Code:
        C:\tmp>cat testargv.py
        import sys
        print sys.argv
        
        C:\tmp>python testargv.py a 2 III
        ['testargv.py', 'a', '2', 'III']

        Comment

        • Deathwing
          New Member
          • Mar 2007
          • 32

          #5
          Originally posted by dshimer
          I just use
          Code:
          python progname.py arg1 arg2
          remember that argv[0] is the program name itself, for example
          Code:
          C:\tmp>cat testargv.py
          import sys
          print sys.argv
          
          C:\tmp>python testargv.py a 2 III
          ['testargv.py', 'a', '2', 'III']

          Can anyone tell me why this code I have made depicted just below doesn't work... I can't figure it out ?


          Code:
          import sys
          
          
          
          def arguments(x1,y1,x2,y2,points,filename):
               if __name__ == '__main__':
                   import sys
               try:
                   arguments(float(sys.argv[1]), float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]), int(sys.argv[5]),sys.argv[6])
                   except (IndexError, TypeError):
                       print "Usage: Too few arguments"
          
          
          f = open(r'points.xyz', 'w')
          
          f.writelines(['%9.2f%14.2f\n' % (random.uniform(xUL,xLR),random.uniform(yLR,yUL))for _ in range(points)])
          
          f.close()
          
          raw_input("\nFinished, Press enter to quit.")

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by Deathwing
            Can anyone tell me why this code I have made depicted just below doesn't work... I can't figure it out ?
            Code:
            import sys
            
            
            
            def arguments(x1,y1,x2,y2,points,filename):
                 if __name__ == '__main__':
                     import sys
                 try:
                     arguments(float(sys.argv[1]), float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]), int(sys.argv[5]),sys.argv[6])
                     except (IndexError, TypeError):
                         print "Usage: Too few arguments"
            
            
            f = open(r'points.xyz', 'w')
            
            f.writelines(['%9.2f%14.2f\n' % (random.uniform(xUL,xLR),random.uniform(yLR,yUL))for _ in range(points)])
            
            f.close()
            
            raw_input("\nFinished, Press enter to quit.")
            Use the following general format
            Code:
            def aFunction(arg):
                pass
            if __name__ == '__main__':
                import sys
                # try
                    # convert sys.argvs
                # except
                    # whatever
                aFuncion(arg)

            Comment

            Working...