From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B476C433E0 for ; Thu, 25 Jun 2020 14:14:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8357E2076E for ; Thu, 25 Jun 2020 14:14:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405376AbgFYOOJ (ORCPT ); Thu, 25 Jun 2020 10:14:09 -0400 Received: from vps0.lunn.ch ([185.16.172.187]:60458 "EHLO vps0.lunn.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405360AbgFYOOE (ORCPT ); Thu, 25 Jun 2020 10:14:04 -0400 Received: from andrew by vps0.lunn.ch with local (Exim 4.94) (envelope-from ) id 1joSdb-002DD5-6Z; Thu, 25 Jun 2020 16:13:59 +0200 Date: Thu, 25 Jun 2020 16:13:59 +0200 From: Andrew Lunn To: Stephen Hemminger Cc: Michal Kubecek , netdev , Chris Healy Subject: Re: [ethtool v2 4/6] Add --json command line argument parsing Message-ID: <20200625141359.GL442307@lunn.ch> References: <20200625001244.503790-1-andrew@lunn.ch> <20200625001244.503790-5-andrew@lunn.ch> <20200624223258.140f6cad@hermes.lan> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200624223258.140f6cad@hermes.lan> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On Wed, Jun 24, 2020 at 10:32:58PM -0700, Stephen Hemminger wrote: > On Thu, 25 Jun 2020 02:12:42 +0200 > Andrew Lunn wrote: > > > diff --git a/internal.h b/internal.h > > index edb07bd..7135140 100644 > > --- a/internal.h > > +++ b/internal.h > > @@ -23,6 +23,8 @@ > > #include > > #include > > > > +#include "json_writer.h" > > + > > #define maybe_unused __attribute__((__unused__)) > > > > /* internal for netlink interface */ > > @@ -221,6 +223,8 @@ struct cmd_context { > > int argc; /* number of arguments to the sub-command */ > > char **argp; /* arguments to the sub-command */ > > unsigned long debug; /* debugging mask */ > > + bool json; /* Output JSON, if supported */ > > + json_writer_t *jw; /* JSON writer instance */ > > You can avoid the boolean by just checking for NULL jw variable. Hi Stephen It is a while since i wrote this code, but i think i considered that. The problem is, only a few commands support json output. I could call json_new() unconditional of if the command actually support json or not, that is not a problem. But then json_destory() should also be unconditionally called. And that does fputs("\n", self->out); So you end up with an extra blank line. Using the boolean allows me to defer json_new()/json_destroy() into the actual commands which supports json. Andrew