我不知道这是否只是一个新手的错误,但Python并没有认识到我的所有论点。我正在使用bash,所以也许这就是问题所在。我有一个名为-vs的工作参数,我试图让它在它之后检测一个必要的整数,所以适当的语法是program.py -vs 5
,或者一些整数。但是,它不承认“5”。当我将短划线( - )放在旁边并以字母开头时,它会这样做。我怎样才能让Python看到这个?
以下是代码片段:
elif arg == "-vs" or "--vertical-spacing":
if int(sys.argv[argnum + 1]) >= 0:
vspacing = int(sys.argv[argnum + 1])
else:
print "Invalid Spacing: Less than zero (Ya can't have negative spacing, silly!)"
sys.exit()
顺便说一句,当循环遍历这些参数时,每次argnum变量都会增加1。 (我正在使用for arg in sys.argv[1:]
。别担心,我将argnum作为0启动并且递增它是循环时我做的第一件事,因此它看不到程序路径和/或名称,或{{1 }}
答案 0 :(得分:1)
以下是argparse的一个例子:
<?php
$mail_to = 'email@email.com'; // specify your email here
// Assigning data from the $_POST array to variables
$name = $_POST['name'];
$mail_from = $_POST['email'];
$phone = $_POST['telephone'];
$message = $_POST['comments'];
// Construct email subject
$subject = 'www.mysite.com Message from visitor ' . $name;
// Construct email body
$body_message = 'From: ' . $name . "\r\n";
$body_message .= 'E-mail: ' . $mail_from . "\r\n";
$body_message .= 'Phone: ' . $phone . "\r\n";
$body_message .= 'Message: ' . $message;
// Construct email headers
$headers = 'From: ' . $mail_from . "\r\n";
$headers .= 'Reply-To: ' . $mail_from . "\r\n";
$mail_sent = mail($mail_to, $subject, $body_message, $headers);
if ($mail_sent == true){ ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. I will get back to you shortly.');
window.location = 'index.html';
</script>
<?php } else { ?>
<script language="javascript" type="text/javascript">
alert('Message not sent. Please, notify the site administrator email@email.com');
window.location = 'index.html';
</script>
<?php
} ?>