如何解决如果没有解析任何参数,则 argparse 正在退出
我正在使用 argparse 并且一些参数需要一个值。 例如,在我的论据中
lst -pln -pk
lst sotore true,-pln 也存储真值, 但是 -pk 需要输入一个值。
但我不想让程序退出。相反,我想显示无效输入错误消息并在我的程序中继续 while 循环。
注意:在我的程序中,我使用了一个while循环来继续程序,直到用户输入exit并按回车。它工作正常。
解决方法
使用let playerid;
let booleanLog = true
let nameInput;
let passInput;
let confirmBtn;
let changeLoginRegisterBtn;
let goldBtn;
let mainSceneEnable=false;
let shopMenuBollean = true;
let shopMenu = ["Store","Forge","Workshop","Enchanter","Inventory","Hero Management","Upgrades"];
let villageMenu = ["Production","Tavern","Adventure Guild","Upgrades"];
let drawSquare= 0
let shop = {
text:"Shop",x:0,y:0,w:0,h:0
}
let village = {
text:"Village",h:0
}
function setup() {
createCanvas(windowWidth,windowHeight);
loginScreen()
}
function draw() {
background(220);
textAlign(CENTER,CENTER)
textSize(20)
//Change Between Login and Register text
if(mainSceneEnable == false){
if (booleanLog){
text("LOGIN",windowWidth*0.52,windowHeight*0.25)
}else{
text("REGISTER",windowHeight*0.25)
}
}else{
MenuHud()
}
}
function mousePressed() {
//Enables in game buttons
if(mainSceneEnable){
MenuButtons();
}
}
function change_Login_Register() {
if(booleanLog){
booleanLog = false
}else {
booleanLog = true
}
}
function loginScreen(){
background(220);
nameInput = createInput('');
nameInput.position(windowWidth*0.45,windowHeight*0.3);
passInput = createInput('');
passInput.position(windowWidth*0.45,windowHeight*0.4);
confirmBtn = createButton('Confirm');
confirmBtn.position(windowWidth*0.45,windowHeight*0.45);
confirmBtn.mousePressed(do_Login);
changeLoginRegisterBtn = createButton('Change Between Login and Register');
changeLoginRegisterBtn.position(windowWidth*0.38,windowHeight*0.17);
changeLoginRegisterBtn.mousePressed(change_Login_Register);
}
function do_Login(){
let username=nameInput.value();
let password=passInput.value();
let player = {
username:username,password:password
}
//Do Login
if (booleanLog){
httpPost('/login','json',player,(dataReceived)=>{
playerid=dataReceived[0].pl_id;
remove_Login();
mainSceneEnable=true;
print(playerid)
});
//Do Register
}else{
httpPost('/register',(dataReceived)=>{
playerid=dataReceived[0].pl_id;
});
}
}
function remove_Login(){
nameInput.remove();
passInput.remove();
confirmBtn.remove();
changeLoginRegisterBtn.remove();
}
function MenuHud(){
textAlign(CENTER,CENTER)
textSize(20)
fill(255)
stroke("red")
strokeWeight(2)
shop.x = width/22;
shop.y = height/6;
shop.w = width/15;
shop.h = height/12;
village.x = width/9;
village.y = height/6;
village.w = width/15;
village.h = height/12;
//Shop or Village Menu options
rect(shop.x,shop.y,shop.w,shop.h)
text(shop.text,shop.x + shop.w/2,shop.y + shop.h/2)
rect(village.x,village.y,village.w,village.h)
text(village.text,village.x + village.w/2,village.y + village.h/2)
//Mouse over to change bettween menus
if(mouseX > shop.x && mouseX < shop.x+shop.w && mouseY > shop.y && mouseY < shop.y+shop.h){
shopMenuBollean = true;
}else if (mouseX > village.x && mouseX < village.x+village.w && mouseY > village.y && mouseY < village.y+village.h){
shopMenuBollean = false;
}
//array for the menu options
textSize(15)
//Shop Menu
if(shopMenuBollean == true) {
for(let i=0; i<shopMenu.length; i++){
rect(width/22,height/4+(i*height/12),width/7.55,height/12)
text(shopMenu[i],width/9,height/3.5+(i*height/12));
}
//Village Menu
}else if(shopMenuBollean == false){
for(let i=0; i<villageMenu.length; i++){
rect(width/22,height/12)
text(villageMenu[i],height/3.5+(i*height/12));
}
}
// Store
if (drawSquare == 1){
rect(width/5.54,height/1.72,width/1.4,height/3.99)
rect(width/5.54,width/10,height/45) //Shop toggle
rect(width/1.6,height/2,width/15,height/15) //NPCs in queue
rect(width/1.4,height/6,width/3.55,height/1.501) //Inventory
}
//Forge
else if (drawSquare == 2){
rect(width/5.54,width/1.8,height/2.43) //Forge Menu
rect(width/4.2,height/5,height/7.5) //Material 1
rect(width/2.6,height/7.5) //Material 2
rect(width/1.89,height/7.5) //Material 3
rect(width/2.55,height/2.72,height/45) //Craft Button
rect(width/2.6,height/2.4,width/8,height/8) //Result
rect(width/1.4,height/1.501) //Inventory
}
//Workshop
else if (drawSquare == 3){
rect(width/5.54,height/3.99) //Workshop Menu
rect(width/4.9,height/1.6,width/11,height/8) //Item
rect(width/3.2,height/8) //Gold
rect(width/2.38,height/8) //Material
rect(width/2.55,height/1.3,height/45) //Upgrade Button
rect(width/1.65,height/1.501) //Inventory
}
//Enchanter
else if (drawSquare == 4){
rect(width/5.54,height/3.99) //Enchant Menu
rect(width/4.9,height/8) //Enchantment
rect(width/2.55,height/45) //Enchant Button
rect(width/1.65,height/1.501) //Inventory
//rect(width/2.6,height/8) //Enchant Preview When Mouse Hover
}
//Inventory
else if (drawSquare == 5){
rect(width/5.54,width/1.25,height/1.501) //Inventory
}
//Heroes Management
else if (drawSquare == 6){
rect(width/5.54,height/1.501) //Hero Menu
rect(width/2.8,width/1.6,height/3.01) //Inventory
rect(width/3.6,height/1.8,height/45) //Clock
rect(width/2.8,height/7.5) //Slot 1
rect(width/2.1,height/7.5) //Slot 2
rect(width/1.68,height/7.5) //Slot 3
rect(width/1.4,height/7.5) //Slot 4
rect(width/1.2,height/7.5) //Slot 5
rect(width/2.8,height/2.5,width/4,height/15) //Time
}
//Upgrades
else if (drawSquare == 7){
rect(width/5.54,height/3.99)
}
//Tavern
else if (drawSquare == 8){
rect(width/5.54,height/3.99)
}
}
function MenuButtons(){
//Shop Menu
if(shopMenuBollean == true) {
//Store
if(mouseX > width/22 && mouseX < width/22+width/7.55 && mouseY > height/4+(0*height/12) && mouseY < height/4+(0*height/12)+height/12){
drawSquare= 1
}
//Forge
else if(mouseX > width/22 && mouseX < width/22+width/7.55 && mouseY > height/4+(1*height/12) && mouseY < height/4+(1*height/12)+height/12){
drawSquare= 2
}
//Workshop
else if(mouseX > width/22 && mouseX < width/22+width/7.55 && mouseY > height/4+(2*height/12) && mouseY < height/4+(2*height/12)+height/12){
drawSquare= 3
}
//Enchanter
else if(mouseX > width/22 && mouseX < width/22+width/7.55 && mouseY > height/4+(3*height/12) && mouseY < height/4+(3*height/12)+height/12){
drawSquare= 4
}
//Inventory
else if(mouseX > width/22 && mouseX < width/22+width/7.55 && mouseY > height/4+(4*height/12) && mouseY < height/4+(4*height/12)+height/12){
drawSquare= 5
}
//Heroes Management
else if(mouseX > width/22 && mouseX < width/22+width/7.55 && mouseY > height/4+(5*height/12) && mouseY < height/4+(5*height/12)+height/12){
drawSquare= 6
}
//Upgrades
if(mouseX > width/22 && mouseX < width/22+width/7.55 && mouseY > height/4+(6*height/12) && mouseY < height/4+(6*height/12)+height/12){
drawSquare= 7
}
// Village Menu
}else if(shopMenuBollean == false) {
//Tavern
if(mouseX > width/22 && mouseX < width/22+width/7.55 && mouseY > height/4+(0*height/12) && mouseY < height/4+(0*height/12)+height/12){
drawSquare= 8
}
//Wood
else if(mouseX > width/22 && mouseX < width/22+width/7.55 && mouseY > height/4+(1*height/12) && mouseY < height/4+(1*height/12)+height/12){
drawSquare= 9
}
//Mine
else if(mouseX > width/22 && mouseX < width/22+width/7.55 && mouseY > height/4+(2*height/12) && mouseY < height/4+(2*height/12)+height/12){
drawSquare= 10
}
}
}
。
来自docs:
ArgumentParser 对象通常将单个命令行参数与要采取的单个操作相关联。 const express = require('express')
const bodyParser = require('body-parser')
const mysql = require('mysql');
const app = express()
const port = 3000
app.use(express.static('public'));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
//creates instance to make connection to the sql database
const db = mysql.createConnection({
host: "localhost",port: 3306,user: "root",password: "root",database: "heroshoppe",//WORKING WITH MAMP ON MAC!!!
});
db.connect(function(err) {
if (err) throw err;
console.log("Connected to DB!");
});
app.post('/register',(req,res)=>{
let username = req.body.username;
let password = req.body.password;
let gold = 1000;
let sql = "SELECT * FROM Player WHERE pl_name='"+username+"'";
db.query(sql,(err,result)=>{
if(err) throw err;
//no player with that name
if(result.length<1){
let sql = "INSERT INTO Player (`pl_name`,`pl_pass`,`pl_gold`) VALUES ('"+username+"','"+password+"','"+gold+"')";
db.query(sql,result)=>{
if(err) throw err;
//if affected rows
let sql = "SELECT pl_id FROM Player WHERE pl_name='"+username+"' AND pl_pass='"+password+"'";
db.query(sql,result)=>{
if(err) throw err;
console.log(result);
res.send(result);
});
});
}
});
});
app.post('/login',res)=>{
let username = req.body.username;
let password = req.body.password;
let sql = "SELECT * FROM Player WHERE pl_name='"+username+"'";
db.query(sql,result)=>{
if(err) throw err;
console.log(result);
if(result.length>1){
let sql = "SELECT pl_id FROM Player WHERE pl_name='"+username+"' AND pl_pass='"+password+"'";
db.query(sql,result)=>{
if(err) throw err;
console.log(result);
res.send(result);
});
}
});
});
app.listen(port,() => {
console.log(`Example app listening at http://localhost:${port}`)
})
关键字参数将不同数量的命令行参数与单个操作相关联。支持的值为:
- (...)
-
nargs="?"
。如果可能,将从命令行使用一个参数,并作为单个项目生成。如果不存在命令行参数,则将生成默认值。
示例:
nargs
'?'
$ cat test.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-pk",nargs="?")
args = parser.parse_args()
print(args)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。