Re: [kvm-unit-tests PATCH 2/2] lib/util,arm,powerpc: replace parse_keyval with better helpers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2016-05-12 10:05+0200, Andrew Jones:
> On Thu, May 12, 2016 at 09:19:08AM +0200, Andrew Jones wrote:
>> On Wed, May 11, 2016 at 10:55:14PM +0200, Radim Krčmář wrote:
>> >  int main(int argc, char **argv)
>> > @@ -115,33 +123,12 @@ int main(int argc, char **argv)
>> >  
>> >  	report_prefix_push("rtas");
>> >  
>> > -	if (argc < 2)
>> > -		report_abort("no test specified");
>> > -
>> > -	report_prefix_push(argv[1]);
>> > -
>> > -	if (strcmp(argv[1], "get-time-of-day") == 0) {
>> > -
>> > -		len = parse_keyval(argv[2], &val);
>> > -		if (len == -1) {
>> > -			printf("Missing parameter \"date\"\n");
>> > -			abort();
>> > -		}
>> > -		argv[2][len] = '\0';
>> > -
>> > +	if (parse_keyval(argc, argv, "get-time-of-day", &val))
>> >  		check_get_time_of_day(val);
>> >  
>> > -	} else if (strcmp(argv[1], "set-time-of-day") == 0) {
>> > -
>> > +	if (find_key(argc, argv, "set-time-of-day"))
>> >  		check_set_time_of_day();
>> >  
>> > -	} else {
>> > -		printf("Unknown subtest\n");
>> > -		abort();
>> > -	}
>> > -
>> > -	report_prefix_pop();
>> > -
>> 
>> Also a nice cleanup. We could have kept the missing parameter abort
>> with something like
>> 
>>  if (find_key(argc, argv, "get-time-of-day")) {
>>      if (!parse_keyval(argc, argv, "get-time-of-day", &val)) {
>>          printf("Missing parameter \"date\"\n");
>>          abort();
>>      }
>>      check_get_time_of_day(val);
>>  }

If checked for the parameter, I'd rather keep it closer to the original:

  if (argc < 3) // there was a bug in the original
      report_abort("")
  if (find_key(2, argv, "get-time-of-day")) {
      if (!parse_keyval(2, argv+2, "date", &val))
          report_abort("");
      check_get_time_of_day(val);
  }

> Hmm, actually the above wouldn't work with the current
> find_key implementation. Maybe we should change it to
> just check for null?

No, that was the point.

>  bool find_key(int argc, char **argv, char *key)
>  {
>     return find_keyval(argc, argv, key) != NULL;
>  }

This is the same as return find_keyval(argc, argv, key), so you could
just use that.

> And change the documentation to explain it looks for @key
> by itself, or with a paired =val, but doesn't parse val.

They are too similar to deserve a distinction.  The previous find_key is
somewhat useful, because the caller doesn't have to remember *key (= can
use a string literal) to distinguish the case when there is no argument.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux