getopt demo

SHELL syntax

#!/bin/bash

Usage(){
    
    
  echo "Usage: -f <file_name> [-d]\n"
  echo "   -f|--file       the hex file to be decoded.\n"
  echo "   -d|--debug      debug\n"
}

ParseCheckOptions(){
    
    
  POSITIONAL=()
  while [[ $# -gt 0 ]]; do
    key="$1"
    case $key in
      -h|--help)
        Usage
        exit 0
        shift # past argument
        shift # past value
        ;;
      -f|--file)
        FILE="$2"
        shift # past argument
        shift # past value
        ;;
      -d|--debug)
        DEBUG="$2"
        shift # past argument
        shift # past value
        ;;
      --default)
        DEFAULT=YES
        shift # past argument
        ;;
      *)
        echo unknown option : $1 
        exit -2
        POSITIONAL+=("$1") # save it in an array for later
        shift # past argument
        ;;
    esac
  done

  set -- "${POSITIONAL[@]}" # restore positional parameters

  [ "${FILE}" == "" ] && echo option \-f requires an argument && exit -1
  echo FILE: ${
    
    FILE}
  echo DEBUG:${
    
    DEBUG}

}

DefaultEnv(){
    
    
  FILE=
  DEBUG=0

  #[ ! -d log ] && mkdir log
}

DoActions(){
    
    
  echo DoActions
}



Main(){
    
    

  startTimeR=`date '+%Y-%m-%d-%H-%M-%S'`
  startTime=`date '+%Y-%m-%d %H:%M:%S'`
  startTime_s=`date +%s`

  DefaultEnv
  ParseCheckOptions $*
  DoActions

  endTime=`date '+%Y-%m-%d %H:%M:%S'`
  endTime_s=`date +%s`

  sumTime=$[ $endTime_s - $startTime_s  ]
  echo "$startTime ---> $endTime"
  echo "Total:$sumTime seconds"

}


Main $*

C syntax

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

static void usage(void)
{
    
    
  fprintf(stderr,
      "Usage: -f <file_name> [-d]\n"
      "   -f|--file       the hex file to be decoded.\n"
      "   -d|--debug      debug\n"
      );
}

static const struct option long_opts[] = {
    
    
  {
    
    "help"    , no_argument       , NULL, 'h'},
  {
    
    "file"    , required_argument , NULL, 'f'},
  {
    
    "debug"   , required_argument , NULL, 'd'},
  {
    
    NULL      , 0                 , NULL, 0  }
};

static int debug = 0;

int main(int argc, char** argv)
{
    
    
  const char * short_opt = ":hf:d";
  int hflag = 0;
  int opt = 0;
  int options_index = 0;
  char * file_name;
  opterr = 0;

  if(argc == 1){
    
    
    usage();
    return -1;
  }

  //If there are no more option characters, getopt() returns -1.
  while((opt = getopt_long(argc,argv,short_opt,long_opts,&options_index)) != -1) {
    
    
    switch(opt)
    {
    
    
      case 'h':
        hflag       = 1     ;break;
      case 'f':
        file_name   = optarg;break;
      case 'd':
        debug       = 1     ;break;
      case '?':
        fprintf(stderr,"Error: unknown option '-%c\n'",optopt);
        return -1;
      case ':':
        fprintf(stderr,"Error: option -%c requires an argument\n", optopt);
        return -2;
      default:
        abort();
    }
  }

  if(hflag || argc == 1)
  {
    
    
    usage();
    return 0;
  }

  if(!file_name){
    
    
    fprintf(stderr,"Error: file name must be specified\n");
    return 1;
  }

  printf("Parameters got: file_name = %s,  debug : %s\n",
            file_name,debug?"YES":"NO");

  return 0;
}

Guess you like

Origin blog.csdn.net/u011011827/article/details/133384792