upower: Add a -b/--battery option to dump only battery information

This is useful when asking people to give information about battery
health. It avoids having to give two-step instructions of first
enumerating devices with `upower -e`, then looking for a device whose
name starts with `/org/freedesktop/UPower/devices/battery_`, then
running `upower -i` on that device.

Signed-off-by: Josh Triplett <josh@joshtriplett.org>
This commit is contained in:
Josh Triplett 2025-07-10 17:24:07 -07:00 committed by Kate Hsuan
parent ff605e51c1
commit 3369d31472
3 changed files with 17 additions and 0 deletions

View file

@ -11,6 +11,7 @@ _enumerate_objects() {
} }
_arguments \ _arguments \
"(--battery -b)"{--battery,-b}"[Dump all parameters for battery objects]" \
"(--enumerate -e)"{--enumerate,-e}"[Enumerate objects paths for devices]" \ "(--enumerate -e)"{--enumerate,-e}"[Enumerate objects paths for devices]" \
"(--dump -d)"{--dump,-d}"[Dump all parameters for all objects]" \ "(--dump -d)"{--dump,-d}"[Dump all parameters for all objects]" \
"(--monitor -m)"{--monitor,-m}"[Monitor activity from the power daemon]" \ "(--monitor -m)"{--monitor,-m}"[Monitor activity from the power daemon]" \

View file

@ -19,6 +19,7 @@
<refsynopsisdiv> <refsynopsisdiv>
<cmdsynopsis> <cmdsynopsis>
<command>upower</command> <command>upower</command>
<arg><option>--battery</option></arg>
<arg><option>--dump</option></arg> <arg><option>--dump</option></arg>
<arg><option>--enumerate</option></arg> <arg><option>--enumerate</option></arg>
<arg><option>--monitor-detail</option></arg> <arg><option>--monitor-detail</option></arg>
@ -42,6 +43,14 @@
<refsect1> <refsect1>
<title>OPTIONS</title> <title>OPTIONS</title>
<variablelist> <variablelist>
<varlistentry>
<term><option>-b</option>, <option>--battery</option></term>
<listitem>
<para>
Dump all parameters for battery devices.
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><option>-d</option>, <option>--dump</option></term> <term><option>-d</option>, <option>--dump</option></term>
<listitem> <listitem>

View file

@ -272,6 +272,7 @@ int
main (int argc, char **argv) main (int argc, char **argv)
{ {
GOptionContext *context; GOptionContext *context;
gboolean opt_battery = FALSE;
gboolean opt_dump = FALSE; gboolean opt_dump = FALSE;
gboolean opt_enumerate = FALSE; gboolean opt_enumerate = FALSE;
gboolean opt_monitor = FALSE; gboolean opt_monitor = FALSE;
@ -287,6 +288,7 @@ main (int argc, char **argv)
UpDevice *device; UpDevice *device;
const GOptionEntry entries[] = { const GOptionEntry entries[] = {
{ "battery", 'b', 0, G_OPTION_ARG_NONE, &opt_battery, _("Dump all parameters for battery objects"), NULL },
{ "dump", 'd', 0, G_OPTION_ARG_NONE, &opt_dump, _("Dump all parameters for all objects"), NULL }, { "dump", 'd', 0, G_OPTION_ARG_NONE, &opt_dump, _("Dump all parameters for all objects"), NULL },
{ "enumerate", 'e', 0, G_OPTION_ARG_NONE, &opt_enumerate, _("Enumerate objects paths for devices"), NULL }, { "enumerate", 'e', 0, G_OPTION_ARG_NONE, &opt_enumerate, _("Enumerate objects paths for devices"), NULL },
{ "monitor", 'm', 0, G_OPTION_ARG_NONE, &opt_monitor, _("Monitor activity from the power daemon"), NULL }, { "monitor", 'm', 0, G_OPTION_ARG_NONE, &opt_monitor, _("Monitor activity from the power daemon"), NULL },
@ -333,6 +335,11 @@ main (int argc, char **argv)
if (opt_enumerate) if (opt_enumerate)
return up_tool_output_enumerate (client); return up_tool_output_enumerate (client);
if (opt_battery) {
device_filter = g_list_append (device_filter, GINT_TO_POINTER (UP_DEVICE_KIND_BATTERY));
opt_dump = TRUE;
}
if (opt_dump) { if (opt_dump) {
retval = up_tool_output_device_dump (client, device_filter); retval = up_tool_output_device_dump (client, device_filter);
g_list_free (device_filter); g_list_free (device_filter);