cli: changing nmcli output to better fit both computer and human needs

The output is basically tabular with fields (columns) presenting specific pieces of info.
Each line represents a single object. It's possible to switch to multiline output using
'--multiline' option. In that mode single object is presented on more lines - each field
on its line.
Terse mode now uses ':' as field separator. It also escapes all occurences of ':' and '\'
inside field values to ease parsing. The escaping behaviour can be controlled through
'--escape' option. By default, escaping is switched on in tabular mode. When using terse
mode ('--terse'), '--fields' option is mandatory for specifying required fields. That helps
for flexibility and backwards compatibility.
Not all output is converted yet.
This commit is contained in:
Jiří Klimeš
2010-03-18 15:39:15 +01:00
parent 15351042ba
commit 55949277bd
6 changed files with 727 additions and 283 deletions

View File

@@ -20,10 +20,12 @@
#ifndef NMC_NMCLI_H
#define NMC_NMCLI_H
#include <glib.h>
#include <nm-client.h>
#include <nm-remote-settings.h>
#include <nm-remote-settings-system.h>
/* nmcli exit codes */
typedef enum {
/* Indicates successful execution */
@@ -51,27 +53,55 @@ typedef enum {
NMC_PRINT_PRETTY
} NMCPrintOutput;
/* === Output fields === */
typedef struct {
const char *name; /* Field's name */
const char *name_l10n; /* Field's name for translation */
int width; /* Width in screen columns */
const char *value; /* Value of current field */
guint32 flags; /* Flags */
} NmcOutputField;
/* Flags for NmcPrintFields */
#define NMC_PF_FLAG_MULTILINE 0x00000001 /* Multiline output instead of tabular*/
#define NMC_PF_FLAG_TERSE 0x00000002 /* Terse outpud mode */
#define NMC_PF_FLAG_PRETTY 0x00000004 /* Pretty output mode */
#define NMC_PF_FLAG_HEADER 0x00000008 /* Print headers instead of values */
#define NMC_PF_FLAG_ESCAPE 0x00000010 /* Escape column separator and '\' */
typedef struct {
GArray *indices; /* Array of field indices to the array of allowed fields */
char *header_name; /* Name of the output */
int indent; /* Indent by this number of spaces */
guint32 flags; /* Various flags for controlling output: see NMC_PF_FLAG_* values */
} NmcPrintFields;
/* NmCli - main structure */
typedef struct _NmCli {
NMClient *client;
NMClient *(*get_client) (struct _NmCli *nmc);
NMClient *client; /* Pointer to NMClient of libnm-glib */
NMClient *(*get_client) (struct _NmCli *nmc); /* Pointer to function for creating NMClient */
NMCResultCode return_value;
GString *return_text;
NMCResultCode return_value; /* Return code of nmcli */
GString *return_text; /* Reason text */
int timeout;
int timeout; /* Operation timeout */
NMRemoteSettingsSystem *system_settings;
NMRemoteSettings *user_settings;
NMRemoteSettingsSystem *system_settings; /* System settings */
NMRemoteSettings *user_settings; /* User settings */
gboolean system_settings_running;
gboolean user_settings_running;
gboolean system_settings_running; /* Is system settings service running? */
gboolean user_settings_running; /* Is user settings service running? */
GSList *system_connections;
GSList *user_connections;
GSList *system_connections; /* List of system connections */
GSList *user_connections; /* List of user connections */
gboolean should_wait;
NMCPrintOutput print_output;
gboolean should_wait; /* Indication that nmcli should not end yet */
NMCPrintOutput print_output; /* Output mode */
gboolean multiline_output; /* Multiline output instead of default tabular */
gboolean escape_values; /* Whether to escape ':' and '\' in terse tabular mode */
char *required_fields; /* Required fields in output: '--fields' option */
NmcOutputField *allowed_fields; /* Array of allowed fields for particular commands */
NmcPrintFields print_fields; /* Structure with field indices to print */
} NmCli;
#endif /* NMC_NMCLI_H */