为什么有多个解析器?

时间:2016-10-29 22:49:53

标签: python argparse

我正在阅读argparse文档:https://docs.python.org/3.4/library/argparse.html

我通过"父母"对于ArgumentParser的doc,它提到当几个解析器共享公共args时使用它。我想知道,我什么时候需要有几个解析器"首先?

1 个答案:

答案 0 :(得分:3)

documentation已经相当简单了:

  

不是重复这些参数的定义,而是可以使用包含所有共享参数并传递给Parent =参数到ArgumentParser的单个解析器

我想到的一个用例就是当你有很多脚本对需要访问用户名和密码的数据库执行不同的操作时。其中一个脚本改变了一个表,另一个脚本改变了一些权限......但是所有这些脚本都需要用户名和密码来访问数据库,对吧?

你可以有一个“基础”参数解析器,它需要(或解析)用户名和密码,然后拥有更多具有更多参数的专用解析器。例如:

$ python ./stack_055_A.py --username foo --password bar --table baz
Received arguments: Namespace(password='bar', table='baz', username='foo')

运行此命令将导致:

#include<stdio.h>
#include<stdlib.h>
#include<sqlite3.h>

static int callback(void *NotUsed, int argc, char **argv, char **azColName){
   int i;
   for(i=0; i<argc; i++){
      printf("%s = %s\t", azColName[i], argv[i] ? argv[i] : "NULL");
   }
   printf("\n");
   return 0;
}

int main(char c, char* argv[])
{
  sqlite3 *db;
  int rc;
  char *zErrMsg = 0;
  char *sql;
  int choice;
  char option;
  int port,priority;
  char *nameID,*ip;

  rc = sqlite3_open("topology.cnf", &db);

  if(rc){
     fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
     return(0);
  }else{
      fprintf(stderr, "Opened database successfully\n");
  }

  // Creation of table
  sql = "CREATE TABLE BROKERLIST("  \
         "IP             CHAR(16)     PRIMARY KEY     NOT NULL," \
         "PORT           INT          NOT NULL," \
         "NAMEID         TEXT         NOT NULL," \
         "PRIORITY       INT          NOT NULL );";

  // Executing SQL statement
  rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
   if( rc != SQLITE_OK ){
   fprintf(stderr, "SQL error: %s\n", zErrMsg);
      sqlite3_free(zErrMsg);
   }else{
      fprintf(stdout, "Table created successfully\n");
   }

   do{
      printf("Enter choice \n1.Add broker\t2.Remove broker\t3.Change broker priority\t4.Show map");
      scanf("%d", &choice);
      switch(choice){
         case 1: printf("\nEnter IP, Port, NameID, Priority");
                 scanf("%s%d%s%d",ip,&port,nameID,&priority);
                 sql = "INSERT INTO BROKERLIST(IP,PORT,NAMEID,PRIORITY) " \
                       "VALUES('"+ip+"','"+port+"','"+nameID+"','"+priority+"'); " ;
                 rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
                 if( rc != SQLITE_OK ){
                        fprintf(stderr, "SQL error: %s\n", zErrMsg);
                        sqlite3_free(zErrMsg);
                 }else{
                        fprintf(stdout, "Added broker successfully\n");
                 }
                 break;
         case 2: printf("\nEnter NameID of the broker to be removed");
                 scanf("%s",nameID);
                 sql = "DELETE from BROKERLIST where NAMEID = '"+nameID+"'; " ;
                 rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
                 if( rc != SQLITE_OK ){
                        fprintf(stderr, "SQL error: %s\n", zErrMsg);
                        sqlite3_free(zErrMsg);
                 }else{
                        fprintf(stdout, "Removed broker successfully\n");
                 }
                 break;
         case 3: printf("\nEnter NameID of broker whose priority has to be changed and the new priority");
                 scanf("%s%d",nameID,&priority);
                 sql = " UPDATE BROKERLIST set PRIORITY = '"+priority+"' where NAMEID = '"+nameID+"'; ";
                 rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
                 if( rc != SQLITE_OK ){
                        fprintf(stderr, "SQL error: %s\n", zErrMsg);
                        sqlite3_free(zErrMsg);
                 }else{
                        fprintf(stdout, "Updated Priority of broker\n");
                 }
                 break;
         case 4: sql = "SELECT * from BROKERLIST" ;
                 rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
                 if( rc != SQLITE_OK ){
                        fprintf(stderr, "SQL error: %s\n", zErrMsg);
                        sqlite3_free(zErrMsg);
                 }
                 break;
      }
      printf("\n Do you want to continue?(Y/N)");
      fflush(stdin);
      scanf("%c",&option);
   }while(option == 'Y' || option == 'y');
   return 0;
}

因此,每次需要用户名和密码时,都可以重复使用 basic \ parser