add --tagn 3 a b c

Flexible way to add tag with N elements:

eg:

empty tag:
  --tagn 0

protected event:
  --tagn 1 -

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin 2024-09-22 09:50:07 +09:00
parent 6e1bfcca7e
commit 74ba45adbc

View File

@ -86,6 +86,7 @@ void usage()
printf(" --pow <difficulty> number of leading 0 bits of the id to mine\n");
printf(" --mine-pubkey mine a pubkey instead of id\n");
printf(" --tag <key> <value> add a tag\n");
printf(" --tagn <N> <tags * N...> add a tag with N elements. eg: `--tagn 3 a b c`, `--tagn 0`\n");
printf(" -e <event_id> shorthand for --tag e <event_id>\n");
printf(" -p <pubkey> shorthand for --tag p <pubkey>\n");
printf(" -t <hashtag> shorthand for --tag t <hashtag>\n");
@ -418,6 +419,54 @@ static int nostr_add_tag(struct nostr_event *ev, const char *t1, const char *t2)
return nostr_add_tag_n(ev, ts, 2);
}
static int nostr_add_arg_tags(struct nostr_event *ev, int *argc, const char **argv[])
{
struct nostr_tag *tag;
int i;
uint64_t n;
uint16_t small_n;
const char *arg;
if (*argc == 0) {
fprintf(stderr, "expected more arguments to --tagn\n");
return 0;
}
arg = *(*argv)++; (*argc)--;
if (!parse_num(arg, &n)) {
fprintf(stderr, "expected X to be a number in --tagn X ...\n");
return 0;
}
if (n > MAX_TAG_ELEMS) {
fprintf(stderr, "too many --tagn arguments (%"PRIu64" > %d)\n", n, MAX_TAG_ELEMS);
return 0;
}
small_n = (uint16_t)n;
if (ev->num_tags + small_n > MAX_TAGS) {
fprintf(stderr, "too many tags (%d + %d > %d)\n", ev->num_tags, small_n, MAX_TAG_ELEMS);
return 0;
}
tag = &ev->tags[ev->num_tags++];
tag->num_elems = 0;
for (i = 0; i < small_n; i++) {
if (*argc <= 0) {
fprintf(stderr, "expected %d tags: --tagn %d ..., got %d args\n", small_n, small_n, i);
return 0;
}
arg = *(*argv)++; (*argc)--;
//fprintf(stderr, "got arg '%s' argc(%d)\n", arg, *argc);
tag->strs[i] = arg;
tag->num_elems++;
}
return 1;
}
static int parse_args(int argc, const char *argv[], struct args *args, struct nostr_event *ev)
{
@ -490,6 +539,11 @@ static int parse_args(int argc, const char *argv[], struct args *args, struct no
fprintf(stderr, "couldn't add t tag");
return 0;
}
} else if (!strcmp(arg, "--tagn")) {
if (!nostr_add_arg_tags(ev, &argc, &argv)) {
return 0;
}
has_added_tags = 1;
} else if (!strcmp(arg, "--tag")) {
has_added_tags = 1;
if (args->tags) {