1

I am writing a program that runs qiime. I need the program to recognize numbers that the user types on the command line, but I think that subprocess.call may not be able to tell that the numbers are integers.

What I have so far:

# Items to import
import subprocess
import argparse
from sys import argv

#Variables
parser=argparse.ArgumentParser()
parser.add_argument('--trim-forward', type=int, required=True)
parser.add_argument('--trim-reverse', type=int, required=True)
parser.add_argument('--truncate-forward', type=int, required=True)
parser.add_argument('--truncate-reverse', type=int, required=True)
parser.add_argument('--max-ee', type=int, required=True)

# Denoise
cmnd = ['qiime', 'dada2', 'denoise-paired', '--i-demultiplexed-seqs', 'paired-end-demux.qza', '--o-table', 'table.qza', '--o-representative-sequences', 'rep-seqs.qza', '--p-trim-left-f {}'.format(argv[2]), '--p-trim-left-r {}'.format(argv[4]), '--p-trunc-len-f {}'.format(argv[6]), '--p-trunc-len-r {}'.format(argv[8]), '--p-max-ee {}'.format(argv[10])]
print('executing {}'.format(''.join(cmnd)))
res = subprocess.call(cmnd)
print('command terminated with status', res)

Running this program returns errors. Any ideas on how I can tell subprocess.call that these are integers, not strings?

Thank you!

Jessica
  • 47
  • 1
  • 8
  • "Running this problem returns errors" doesn't help us. Show us _what_ the errors are. Also, what makes you think "that subprocess.call may not be able to tell that the numbers are integers"? Whether you're right or not, we need the information that made you guess that, not your guess. – abarnert Mar 12 '18 at 23:30
  • If argv[2] is 1, I get Error: no such option: --p-trim-left-f 1. It was just a guess that the issue was subprocess not recognizing 1 as an integer. The solution provided by @wim fixed this for me. – Jessica Mar 12 '18 at 23:42

1 Answers1

0

This is not really about integers/strings but actually about the parsing of the options with spaces.

In all the places where you have

"--some-option {}".format(some_value)

Change it to:

"--some-option={}".format(some_value)
wim
  • 338,267
  • 99
  • 616
  • 750
  • Are you sure? `argparse` can take spaces after long args. So can tools that use GNU getoptlong, and the standard or most popular parsers for most other modern languages (Go, Rust, Ruby, Node.js, etc.). There are some older (C) tools with custom parsers that take `--` longopts but don't quite follow the GNU standards, and they will have problems with this, but that wouldn't be my first guess. – abarnert Mar 12 '18 at 23:30
  • @abarnert call it a lucky guess ;) – wim Mar 12 '18 at 23:37