From: ori@eigenstate.org
To: 9front@9front.org
Subject: aux/getflags: name the flags
Date: Fri, 17 Apr 2020 14:11:47 -0700 [thread overview]
Message-ID: <558AF1465AE5A5961A49B8327F2FEBA1@eigenstate.org> (raw)
I dislike using aux/getflags, because the flag
names aren't under my control. So I patched it:
% flagfmt='b:bullshit,s:stuff,f:dobinary binary,r res,x width'
% 6.getflags -bsflags and some args
bullshit=()
stuff=()
dobinary=()
flagr=()
flagx=()
bullshit=1
stuff=1
dobinary=(lags)
*=(and some args)
status=''
I'm not sure if this should be committed -- but the code is here:
diff -r 247ac32522a0 sys/src/cmd/aux/getflags.c
--- a/sys/src/cmd/aux/getflags.c Wed Apr 15 18:16:22 2020 +0200
+++ b/sys/src/cmd/aux/getflags.c Fri Apr 17 14:10:28 2020 -0700
@@ -1,5 +1,6 @@
#include <u.h>
#include <libc.h>
+#include <ctype.h>
void
usage(void)
@@ -22,6 +23,21 @@
return nil;
}
+char*
+argname(char *p)
+{
+ Rune r;
+ int n;
+
+ while(1){
+ n = chartorune(&r, p);
+ if(!isalpharune(r) && !isdigitrune(r))
+ break;
+ p += n;
+ }
+ return p;
+}
+
int
countargs(char *p)
{
@@ -39,7 +55,7 @@
void
main(int argc, char *argv[])
{
- char *flags, *p, buf[512];
+ char *flags, *p, *s, *e, buf[512];
int i, n;
Fmt fmt;
@@ -55,19 +71,38 @@
}
fmtfdinit(&fmt, 1, buf, sizeof buf);
- for(p=flags; p!=(char*)1; p=strchr(p, ',')+1)
- fmtprint(&fmt, "flag%.1s=()\n", p);
+ for(p=flags; p!=(char*)1 && *p != 0; p=strchr(p, ',')+1){
+ s = e = nil;
+ if(p[1] == ':'){
+ s = p + 2;
+ e = argname(s);
+ }
+ if(s != e)
+ fmtprint(&fmt, "%.*s=()\n", (int)(e - s), s);
+ else
+ fmtprint(&fmt, "flag%.1s=()\n", p);
+ }
ARGBEGIN{
default:
if((p = findarg(flags, ARGC())) == nil)
usage();
p += runelen(ARGC());
+ s = p + 1;
+ e = p + 1;
+ if(*p == ':' && (e = argname(s)) != s)
+ p = e;
if(*p == ',' || *p == 0){
- fmtprint(&fmt, "flag%C=1\n", ARGC());
+ if(s != e)
+ fmtprint(&fmt, "%.*s=1\n", (int)(e - s), s);
+ else
+ fmtprint(&fmt, "flag%C=1\n", ARGC());
break;
}
n = countargs(p);
- fmtprint(&fmt, "flag%C=(", ARGC());
+ if(s != e)
+ fmtprint(&fmt, "%.*s=(", (int)(e - s), s);
+ else
+ fmtprint(&fmt, "flag%C=(", ARGC());
for(i=0; i<n; i++)
fmtprint(&fmt, "%s%q", i ? " " : "", EARGF(usage()));
fmtprint(&fmt, ")\n");
diff -r 247ac32522a0 sys/src/cmd/aux/usage.c
--- a/sys/src/cmd/aux/usage.c Wed Apr 15 18:16:22 2020 +0200
+++ b/sys/src/cmd/aux/usage.c Fri Apr 17 14:10:28 2020 -0700
@@ -31,6 +31,9 @@
single = 0;
for(p=flags; *p; ){
p += chartorune(&r, p);
+ if(*p == ':')
+ while(*p != '\0' && *p != ',' && *p != ' ')
+ p++;
if(*p == ',' || *p == 0){
if(!single){
fmtprint(&fmt, " [-");
next reply other threads:[~2020-04-17 21:11 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-17 21:11 ori [this message]
2020-04-18 16:22 ` [9front] " cinap_lenrek
2020-04-18 18:24 ` ori
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=558AF1465AE5A5961A49B8327F2FEBA1@eigenstate.org \
--to=ori@eigenstate.org \
--cc=9front@9front.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).