Main function with parameter C programming template

Source: Internet
Author: User
Tags bool printf strcmp
/* A C language design template of the main parametric program, you can automatically sort the parameters into the structure by simply inputting the properties and contents of each parameter in the initialization of the structure. Command execution method such as: Program name-a parameter 1-b parameter 2 program name-?   
 [-H] * * #include "stdafx.h" #define Max_para 6 struct parameter {   
 int id;//parameter number int content_num;//the number of options followed by this argument bool ishave_prefix;//only accept parameter prefixes that must use-XX.   
 Char prefix[32];   
 Char content[256];   
 BOOL Ishave_default_c;   
 Char default_content[256];   
 BOOL isnot_optional;   
BOOL Isinput;   
       
}; struct parameter para_list[max_para]={//parameter number, number of parameter options, parameter prefix (s), parameter prefix, parameter option content,//whether there is a default value, default value, whether the parameter is required, whether the argument has been obtained (based on user's loss    
       
 Before modifying) {1,1,true, "s", "", False, "", True,false},//-s {2,1,true, "-C", "", False, "", True,false},//-c {3,1,true, "K", "", False, "", True,false},//-k {4,1,true, "-T", "", True, "NORMAL", False,false},//-t {5, 1,   
True, "-M", "", False, "", False,false},//-m {6,1,true, "-P", "", False, "", False,false}//-p}; int Generate_parameter (int Argc,char * * argv) {if (argc==1) | | ((argc==2) && (strcmp (argv[1], "-H") ==0 | | strcmp (ARGV[1], "-H") ==0 | | strcmp (ARGV[1), "-?"   
 ==0))) printf ("is help");   
  else {int i,j,k; for (i=1;i<argc;i++) {for (j=0;j<max_para;j++) {if (strcmp (argv[i],para_list[j].prefix) ==0) The input parameter prefix is allowed in the list in the presence of {if (Para_list[j].   
     Isinput)//has already had this parameter before.   
      {printf ("the"%s\ "Can only input once.\n", para_list[j].prefix);   
     Return (-1); else//Normal included parameter {Para_list[j].   
      Isinput=true; if (para_list[j].content_num>= (argc-i))//detects whether the required parameters are not fully entered in {printf ("the \"%s\ "incomplete.\n", Para_lis   
       T[j].prefix);   
      Return (-1);   
      } para_list[j].content[0]= '; for (k=0;k<para_list[j].content_num;k++) sprintf (para_list[j].content, "%s%s", Para_list[j].content,argv[i+1+k)   
      ); I+=para_list[j].content_num; PARA_LIST[J].   
      Isinput=true;   
     break;//jumps out of the for loop. "}} if (J>=max_para)//parameter prefix input error, if there is no prefix parameter, you should add code {printf (" the \ "%s\" is error!\n ", a   
    Rgv[i]);   
   Return (-1);   
  }//The following code checks for required parameters. for (i=0;i<max_para;i++) {if (Para_list[i]. Isnot_optional==true && Para_list[i].   
    Isinput==false)//required option not entered {printf ("the \"%s\ "must be input.\n", para_list[i].prefix);   
   Return (-1);   
}} return 0;   
 int main (int argc, char * * argv) {if (Generate_parameter (ARGC,ARGV)!=0) return-1; User code return 0;

This article is from the "Tommy (Data Recovery)" blog, please be sure to keep this source http://zhangyu.blog.51cto.com/197148/159013

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.