Asked  7 Months ago    Answers:  5   Viewed   44 times

How do I have a Python script that a) can accept user input and how do I make it b) read in arguments if run from the command line?

 Answers

42

To read user input you can try the cmd module for easily creating a mini-command line interpreter (with help texts and autocompletion) and raw_input (input for Python 3+) for reading a line of text from the user.

text = raw_input("prompt")  # Python 2
text = input("prompt")  # Python 3

Command line inputs are in sys.argv. Try this in your script:

import sys
print (sys.argv)

There are two modules for parsing command line options: optparse (deprecated since Python 2.7, use argparse instead) and getopt. If you just want to input files to your script, behold the power of fileinput.

The Python library reference is your friend.

Tuesday, June 1, 2021
 
braindamage
answered 7 Months ago
43

If you want to get the arguments fed to your program on the command line, use the (since Fortran 2003) standard intrinsic subroutine GET_COMMAND_ARGUMENT. Something like this might work

PROGRAM MAIN  
     REAL(8)    :: A,B
     integer :: num_args, ix
     character(len=12), dimension(:), allocatable :: args

     num_args = command_argument_count()
     allocate(args(num_args))  ! I've omitted checking the return status of the allocation 

     do ix = 1, num_args
         call get_command_argument(ix,args(ix))
         ! now parse the argument as you wish
     end do

     PRINT*, A+B, COMMAND_ARGUMENT_COUNT()
END PROGRAM MAIN

Note:

  • The second argument to the subroutine get_command_argument is a character variable which you'll have to parse to turn into a real (or whatever). Note also that I've allowed only 12 characters in each element of the args array, you may want to fiddle around with that.
  • As you've already figured out read isn't used for reading command line arguments in Fortran programs.

Since you want to read an array of real numbers, you might be better off using the approach you've already figured out, that is reading them from the terminal after the program has started, it's up to you.

Saturday, June 19, 2021
 
msg
answered 6 Months ago
msg
39

Use the getopts builtin:
here's a tutorial

pages=  length=  time=

while getopts p:l:t: opt; do
  case $opt in
  p)
      pages=$OPTARG
      ;;
  l)
      length=$OPTARG
      ;;
  t)
      time=$OPTARG
      ;;
  esac
done

shift $((OPTIND - 1))

shift $((OPTIND - 1)) shifts the command line parameters so that you can access possible arguments to your script, i.e. $1, $2, ...

Tuesday, July 27, 2021
 
Parfait
answered 5 Months ago
97

Try with this

msiexec -i "myinstaller.msi" MYDIRPATH=`"C:new folderdata.txt`"

The escape character in PowerShell is the grave-accent(`).

Thursday, August 5, 2021
 
tim_d
answered 5 Months ago
62

Just test the variable line for empty each time you read a line. If the use presses enter with no other data, then line will be empty.

#include <iostream>
#include <string>

using std::cin;
using std::getline;
using std::string;

int main(int argc, char *argv[]) {

    string line;

    while (true) {
        getline(cin, line);
        if (line.empty()) {
            break;
        }
        // some code
    }
    return 0;
}
Friday, August 27, 2021
 
Revent
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share