放置按键和消息的正确方法是什么,无需重复太多代码(expl3)

放置按键和消息的正确方法是什么,无需重复太多代码(expl3)

我正在尝试将无效的错误消息keys放在包中,一些键可用于包,其他键可以通过命令访问\mypkgset,而其他键只能作为某些命令的选项使用。

我放置的消息类似,我想知道是否有更有效的方法来做到这一点。这是我的示例文件:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{mypkg.sty}
\RequirePackage{l3keys2e}
\RequirePackage{xparse}
\ProvidesExplPackage{mypkg}{2019/09/26}{0.1}{A dummy pkg}
% keys for pkg options, only mykey-a, print-b and pkgkey
\keys_define:nn { mypkg }
  {
    mykey-a .tl_set:N         = \l_dummy_a_keya_tl,
    mykey-a .initial:n        = value-a,
    mykey-a .value_required:n = true,
    print-b .bool_set:N       = \l_dummy_b_bool,
    print-b .initial:n        = false,
    print-b .default:n        = true,
    pkgkey  .tl_set:N         = \l_dummy_pkg_key_tl,
    pkgkey  .initial:n        = pkg-value,
    pkgkey  .value_required:n = true
  }
\ProcessKeysOptions { mypkg }
% Add more keys for mypkgset
\keys_define:nn { mypkg }
  {
    print-a .bool_set:N       = \l_dummy_a_bool,
    print-a .initial:n        = false,
    print-a .default:n        = true,
    mykey-b .tl_set:N         = \l_dummy_b_keyb_tl,
    mykey-b .initial:n        = value-b,
    mykey-b .value_required:n = true,
    mykey-c .tl_set:N         = \l_dummy_c_keyc_tl,
    mykey-c .initial:n        = value-c,
    mykey-c .value_required:n = true,
    print-c .bool_set:N       = \l_dummy_c_bool,
    print-c .initial:n        = false,
    print-c .default:n        = true
  }
% \mypkgset
\NewDocumentCommand { \mypkgset } { m }
  { \keys_set:nn { mypkg } {#1} }

% Set keys for \dummya
\keys_define:nn { mypkg / dummy-a }
  {
    print-a .meta:nn   = { mypkg } { print-a = #1 },
    print-a .default:n = true, % need, if not print-a show Error
    mykey-a .meta:nn   = { mypkg } { mykey-a = #1 },
    unknown .code:n    = { \__mypkg_parse_dummy_a_keys:n {#1} }
  }
% Parse unknown keys for \dummya
\cs_new_protected:Npn \__mypkg_parse_dummy_a_keys:n #1
  { \exp_args:NV \__mypkg_parse_dummy_a_keys:nn \l_keys_key_tl {#1} }
\cs_new_protected:Npn \__mypkg_parse_dummy_a_keys:nn #1 #2
  {
    \tl_if_blank:nTF {#2}
      { \msg_error:nnn { mypkg } { dummy-a-key-unknown } {#1} }
      { \msg_error:nnnn { mypkg } { dummy-a-key-value-unknown } {#1} {#2} }
  }
% Errors for  \dummya
\msg_new:nnnn { mypkg } { dummy-a-key-unknown }
  { The~key~'#1'~is~unknown~by~'\c_backslash_str dummya'~and~is~being~ignored.}
  { The~command~'\c_backslash_str dummya'~does~not~have~a~key~called~'#1'.}
\msg_new:nnnn { mypkg } { dummy-a-key-value-unknown }
  { The~key~'#1=#2'~is~unknown~by~'\c_backslash_str dummya'~and~is~being~ignored. }
  { The~command~'\c_backslash_str dummya'~does~not~have~a~key~called~'#1'.}

% Set keys for \dummyb
\tl_new:N \l_mypkg_txt_tl
\keys_define:nn { mypkg / dummy-b }
  {
    print-b .meta:nn   = { mypkg } { print-b = #1 },
    print-b .default:n = true, % need, if not print-b show Error
    mykey-b .meta:nn   = { mypkg } { mykey-b = #1 },
    mytext  .code:n    = { \tl_set:Nn \l_mypkg_txt_tl {#1} },% only in \dummyb
    unknown .code:n    = { \__mypkg_parse_dummy_b_keys:n {#1} }
  }
% Parse unknown keys for \dummyb
\cs_new_protected:Npn \__mypkg_parse_dummy_b_keys:n #1
  { \exp_args:NV \__mypkg_parse_dummy_b_keys:nn \l_keys_key_tl {#1} }
\cs_new_protected:Npn \__mypkg_parse_dummy_b_keys:nn #1 #2
  {
    \tl_if_blank:nTF {#2}
      { \msg_error:nnn { mypkg } { dummy-b-key-unknown } {#1} }
      { \msg_error:nnnn { mypkg } { dummy-b-key-value-unknown } {#1} {#2} }
  }
% Errors for  \dummyb
\msg_new:nnnn { mypkg } { dummy-b-key-unknown }
  { The~key~'#1'~is~unknown~by~'\c_backslash_str dummyb'~and~is~being~ignored.}
  { The~command~'\c_backslash_str dummyb'~does~not~have~a~key~called~'#1'.}
\msg_new:nnnn { mypkg } { dummy-b-key-value-unknown }
  { The~key~'#1=#2'~is~unknown~by~'\c_backslash_str dummyb'~and~is~being~ignored. }
  { The~command~'\c_backslash_str dummyb'~does~not~have~a~key~called~'#1'.}

% Set keys for envdummy
\keys_define:nn { mypkg / dummy-env }
  {
    print-c .meta:nn   = { mypkg } { print-c = #1 },
    print-c .default:n = true, % need, if not print-c show Error
    mykey-c .meta:nn   = { mypkg } { mykey-c = #1 },
    unknown .code:n    = { \__mypkg_parse_dummy_env_keys:n {#1} }
  }

% Parse unknown keys for envdummy
\cs_new_protected:Npn \__mypkg_parse_dummy_env_keys:n #1
  { \exp_args:NV \__mypkg_parse_dummy_env_keys:nn \l_keys_key_tl {#1} }
\cs_new_protected:Npn \__mypkg_parse_dummy_env_keys:nn #1 #2
  {
    \tl_if_blank:nTF {#2}
      { \msg_error:nnn { mypkg } { dummy-env-key-unknown } {#1} }
      { \msg_error:nnnn { mypkg } { dummy-env-key-value-unknown } {#1} {#2} }
  }
% Errors for  envdummy
\msg_new:nnnn { mypkg } { dummy-env-key-unknown }
  { The~key~'#1'~is~unknown~by~'envdummy'~environment~and~is~being~ignored.}
  { The~environment~'envdummy'~does~not~have~a~key~called~'#1'.}
\msg_new:nnnn { mypkg } { dummy-env-key-value-unknown }
  { The~key~'#1=#2'~is~unknown~by~'envdummy'~environment~and~is~being~ignored.}
  { The~environment~'envdummy'~does~not~have~a~key~called~'#1'.}

% dummy-a
\cs_new_protected:Npn \__mypkg_dummy_a:n #1
  {
    This~is~\texttt{"#1"}~with~\texttt{"mykey-a=\tl_use:N \l_dummy_a_keya_tl"}~
    and~\texttt{"\bool_if:nTF { \l_dummy_a_bool } { print-a=true } { print-a=false }"}
  }

% dummy-b
\cs_new_protected:Npn \__mypkg_dummy_b:n #1
  {
    This~is~\texttt{"#1"}~with~\texttt{"mykey-b=\tl_use:N \l_dummy_b_keyb_tl"}~
    and~\texttt{"\bool_if:nTF { \l_dummy_b_bool } { print-b=true } { print-b=false }"}\par
    and~\texttt{"mytext=\tl_use:N \l_mypkg_txt_tl"}
  }

% envdummy
\cs_new_protected:Npn \__mypkg_dummy_env:
  {
    with~\texttt{"mykey-c=\tl_use:N \l_dummy_c_keyc_tl"}~
    and~\texttt{"\bool_if:nTF { \l_dummy_c_bool } { print-c=true } { print-c=false }"}
  }

\NewDocumentCommand { \dummya }{ O{} m }
  {
    \group_begin:
    \IfValueT {#1} { \keys_set:nn {  mypkg / dummy-a } {#1} }
      \__mypkg_dummy_a:n {#2}
    \group_end:
  }
\NewDocumentCommand { \dummyb }{ O{} m }
  {
    \group_begin:
    \IfValueT {#1} { \keys_set:nn {  mypkg / dummy-b } {#1} }
      \__mypkg_dummy_b:n {#2}
    \group_end:
  }
\NewDocumentEnvironment { envdummy } { O{} b}
  {
    \group_begin:
      \IfValueT {#1} { \keys_set:nn {  mypkg / dummy-env } {#1} }
      This~is~\texttt{"#2"}~
  }
  {
      \__mypkg_dummy_env:
    \group_end:
  }
\end{filecontents*}
\usepackage[print-b,pkgkey=some]{mypkg}
\begin{document}
\dummya[mykey-a=other-a,print-a]{A dummya test 1}\par
\dummyb[mykey-b=other-b,mytext={A text for test}]{A dummyb test 1}\par
\begin{envdummy}[mykey-c=other-c]
A envdummy test 1
\end{envdummy}

\mypkgset{muu=kkkk}
\end{document}

相关内容