Shell parameter practice code

the component of shellpkg

.inf 

[Defines]
  INF_VERSION                    = 0x00010006
  BASE_NAME                      = ShellParamApp
  FILE_GUID                      = 079E8E98-AE93-4b9a-8A71-1DC869F23E0A
  MODULE_TYPE                    = UEFI_APPLICATION
  VERSION_STRING                 = 1.0
  ENTRY_POINT                    = ShellCEntryLib

#
# The following information is for reference only and not required by the build tools.
#
#  VALID_ARCHITECTURES           = IA32 X64 EBC
#

[Sources]
  ShellParamApp.c

[Packages]
  MdePkg/MdePkg.dec
  ShellPkg/ShellPkg.dec
  MdeModulePkg/MdeModulePkg.dec

[LibraryClasses]
  ShellCEntryLib
  UefiLib
  ShellLib
  ShellCommandLib
 

.C file

#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/DebugLib.h>
#include <Library/ShellCEntryLib.h>
#include <Library/ShellLib.h>         //brnxxxx 20240429
#include <Library/ShellCommandLib.h>  //brnxxxx 20240429


STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
  {L"-aa", TypeValue},
  {L"-cc", TypeValue},
  {NULL, TypeMax}
};

/**
  UEFI application entry point which has an interface similar to a
  standard C main function.

  The ShellCEntryLib library instance wrappers the actual UEFI application
  entry point and calls this ShellAppMain function.

  @param  Argc             Argument count
  @param  Argv             The parsed arguments

  @retval  0               The application exited normally.
  @retval  Other           An error occurred.

**/
INTN
EFIAPI
ShellAppMain(
  IN UINTN   Argc,
  IN CHAR16** Argv
)
{
  EFI_STATUS      Status;

  LIST_ENTRY* Package;
  CHAR16* ProblemParam;

  CONST CHAR16* ParamAA;
  CONST CHAR16* ParamCC;
  UINT16          intParamAA = 0;
  UINT16          intParamCC = 0;

  ProblemParam = NULL;

  Status = ShellInitialize();
  if (EFI_ERROR(Status)) {
    DEBUG((EFI_D_INFO, "[ZK] !!!ShellInitialize Fail!,Status :%r.\n", Status));
  }
  Status = CommandInit();
  if (EFI_ERROR(Status)) {
    DEBUG((EFI_D_INFO, "[ZK] !!!CommandInit Fail!,Status :%r.\n", Status));
  }

  Status = ShellCommandLineParse(ParamList, &Package, &ProblemParam, TRUE);

  if (EFI_ERROR(Status)) {
    DEBUG((EFI_D_INFO, "[ZK] !!!ShellCommandLineParse Fail!,Status :%r.\n", Status));
  }
  else {
    if (ShellCommandLineGetFlag(Package, L"-aa")) { //-a
      ParamAA = ShellCommandLineGetValue(Package, L"-aa");
      if (ParamAA != NULL) {
        DEBUG((EFI_D_INFO, "ParamAA = %s\n", ParamAA));
        intParamAA = (UINT8)ShellStrToUintn(ParamAA);
      }
    }
    if (ShellCommandLineGetFlag(Package, L"-cc")) { //-b
      ParamCC = ShellCommandLineGetValue(Package, L"-cc");
      if (ParamAA != NULL) {
        DEBUG((EFI_D_INFO, "ParamCC = %s\n", ParamCC));
        intParamCC = (UINT8)ShellStrToUintn(ParamCC);
      }
    }
    // free the command line package
    ShellCommandLineFreeVarList(Package);
  }

  Print(L"intParamAA = %d,intParamCC = %d\n", intParamAA, intParamCC);
  Print(L"-----------------------------------\n");

  return EFI_SUCCESS;
}
 

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值