I’ve created a simple package with options settable via l3keys2e. I was able to figure out how to make the syntax
\usepackage[format = international]{phone}
work, but I’d like to make the syntax
\usepackage[international]{phone}
be a synonym. The method I’m using in the code below works, but is very repetitive, especially when I’ve got more formatting options. Is there a simpler way?
Here’s the code:
\RequirePackage{expl3, l3keys2e, xparse}
\ProvidesExplPackage
{phone}{2013/06/19}{0.1}{Format phone numbers}
\cs_new_nopar:Npn \phone_international_fmt:NNN #1#2#3
{ +1 - #1 - #2 - #3 }
\cs_new_nopar:Npn \phone_parendash_fmt:NNN #1#2#3
{ (#1) \nobreakspace #2 - #3 }
\keys_define:nn { phone }
{
format .choice_code:n =
{
\cs_new_eq:Nc \phone_fmt:NNN
{ phone_ \tl_use:N \l_keys_choice_tl _fmt:NNN }
},
format .generate_choices:n =
{
international,
parendash,
},
% These next lines will become repetitive if I have many more formatting options
international .meta:n = { format = international },
parendash .meta:n = { format = parendash },
}
\ProcessKeysOptions { phone }
\NewDocumentCommand \phone
{ >{ \SplitArgument {2} {-} } m }
{ \phone_fmt:NNN #1 }
\endinput
A minimal example using this package looks like this:
\documentclass{article}
% \usepackage[parendash]{phone}
\usepackage[format = parendash]{phone}
\begin{document}
\phone{212-555-1212}
\end{document}
This yields the desired result
(212) 555-1212
\cs_new_nopar:Npn \phone_international_fmt:NNNisn't really encouraged, and I'd suggest\cs_new:Npn \phone_international_fmt:NNNinstead. Apart from a few specific internals, we've standardised on a position that functions with arguments should be 'long'. – Joseph Wright Jun 20 '13 at 07:46:NNNargument spec rather be:nnn? – cgnieder Jun 20 '13 at 08:04:NNNshould be:nnn– egreg Jun 20 '13 at 08:55xparseis a different thing. At the document level it's most sensible to be restrictive as standard (most document commands don't make sense for multiple paragraphs), but at the code level this is different. A reasonable number of LaTeX bugs were caused by internal code not accepting\partokens. – Joseph Wright Jun 20 '13 at 14:50\partokens should be forbidden. But now I’ll make that choice intentionally, and know what the default should be; thanks. – J. C. Salomon Jun 20 '13 at 14:53\partoken from an internal function is really not worth it. Much better to pick up at the document level and know that at the code level everything is 'long'. – Joseph Wright Jun 20 '13 at 17:14xparse-generated function\phoneis the only thing that calls these internal functions; is that insufficient? Or is allowing long arguments just that much safer? – J. C. Salomon Jun 20 '13 at 18:06