Merge ~paelzer/ubuntu/+source/chrony:lp-1898000-argparse-GROOVY into ubuntu/+source/chrony:ubuntu/groovy-devel

Proposed by Christian Ehrhardt 
Status: Merged
Approved by: Christian Ehrhardt 
Approved revision: 04985296cb82932c936686744213deb937918a79
Merged at revision: 04985296cb82932c936686744213deb937918a79
Proposed branch: ~paelzer/ubuntu/+source/chrony:lp-1898000-argparse-GROOVY
Merge into: ubuntu/+source/chrony:ubuntu/groovy-devel
Diff against target: 35 lines (+10/-6)
2 files modified
debian/changelog (+6/-0)
debian/chronyd-starter.sh (+4/-6)
Reviewer Review Type Date Requested Status
Andreas Hasenack Approve
Canonical Server Pending
git-ubuntu developers Pending
Review via email: mp+391867@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Christian Ehrhardt  (paelzer) wrote :
Revision history for this message
Andreas Hasenack (ahasenack) wrote :

Taking a look

Revision history for this message
Bryce Harrington (bryce) wrote :

A pattern I often use with bash scripts is to pre-process the args to handle long args and other oddities, and then run it through getopts for the full processing. So for example:

for arg in "${@}"; do
    shift
    case "${arg}" in
        "-1")
            set -- "${@}" "\"-1\""
            ;;
        "--long-opt-with-val")
            set -- "${@}" "-L"
            ;;
        *)
            set -- "${@}" "${arg}"
    esac
done

Once you've handled your corner cases, you can pass "${@}" to getopts normally. Below is a fully detailed example:

#!/bin/bash

for arg in "${@}"; do
    shift
    case "${arg}" in
        "-1")
            set -- "${@}" "\"-1\""
            ;;
        "--long-opt-with-val")
            set -- "${@}" "-L"
            ;;
        *)
            set -- "${@}" "${arg}"
    esac
done

# Now, if desired, can safely run the args through getopts:
OPTIND=1
while getopts ":xdF:L:" opt "${@}"; do
    case "${opt}" in
        x) echo "-x is set" ;;
        d) echo "-d is set" ;;
        F) echo "-F = ${OPTARG}" ;;
        L) echo "Long ${OPTARG}" ;;
    esac
done
shift $(( OPTIND - 1 ))

echo "Remaining args: ${@}"

Output:

$ /tmp/chronyd-starter-x.sh -F 1 -x
-F = 1
-x is set
Remaining args:
$ /tmp/chronyd-starter-x.sh -F -1 -x
-F = "-1"
-x is set
Remaining args:
$ /tmp/chronyd-starter-x.sh -dx -F -1 foo bar
-d is set
-x is set
-F = "-1"
Remaining args: foo bar
$ /tmp/chronyd-starter-x.sh -xF -1 foo bar
-x is set
-F = "-1"
Remaining args: foo bar
$ /tmp/chronyd-starter-x.sh --long-opt-with-val foo -dx -F 1 bar baz
Long foo
-d is set
-x is set
-F = 1
Remaining args: bar baz

Revision history for this message
Christian Ehrhardt  (paelzer) wrote :

Thanks Bryce, now we have three options that work :-)

We only want to know if -x is set and nothing else - and we don't want to have to know what potential options there are - in the case above I don't want to care if one day there is a valid "-V 8" and update the getopt string.
To be clear, for full parsing your suggestion is great, but that isn't what I want/need here.

Therefore I'd prefer the small version I have proposed which achieves exactly that and nothing more but therefore also saves some complexities and hopefully will need no updates (that we might forget on merges) down the road.

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

abstain, Bryce was much more thorough :)

review: Abstain
Revision history for this message
Bryce Harrington (bryce) wrote :

@Andreas, nah he didn't like my suggestion, so review back to you.

Revision history for this message
Simon Déziel (sdeziel) wrote :

I too like the simplicity of grep and can confirm the proposed code works so thanks!

Revision history for this message
Simon Déziel (sdeziel) :
Revision history for this message
Christian Ehrhardt  (paelzer) wrote :

> > +for arg in $@; do
> > + echo ARG = $arg
>
> Looks like a debug statement that was left behind.
>

That is absolutely correct and fixed now (force push).

Revision history for this message
Christian Ehrhardt  (paelzer) wrote :

@Andreas - I think we are waiting for your ack?

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

I can see how insisting on getopts will make this complicated, fast, so +1 on the grep approach. Simple to understand.

review: Approve
Revision history for this message
Christian Ehrhardt  (paelzer) wrote :

Thanks, tagged and uploaded

To ssh://git.launchpad.net/~usd-import-team/ubuntu/+source/chrony
 * [new tag] upload/3.5.1-1ubuntu2 -> upload/3.5.1-1ubuntu2

Uploading to ubuntu (via ftp to upload.ubuntu.com):
  Uploading chrony_3.5.1-1ubuntu2.dsc: done.
  Uploading chrony_3.5.1-1ubuntu2.debian.tar.xz: done.
  Uploading chrony_3.5.1-1ubuntu2_source.buildinfo: done.
  Uploading chrony_3.5.1-1ubuntu2_source.changes: done.
Successfully uploaded packages.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/debian/changelog b/debian/changelog
2index 72c5eb4..1bcdf43 100644
3--- a/debian/changelog
4+++ b/debian/changelog
5@@ -1,3 +1,9 @@
6+chrony (3.5.1-1ubuntu2) groovy; urgency=medium
7+
8+ * d/chronyd-starter.sh: fix commandline argument parsing (LP: #1898000)
9+
10+ -- Christian Ehrhardt <christian.ehrhardt@canonical.com> Tue, 06 Oct 2020 12:20:40 +0200
11+
12 chrony (3.5.1-1ubuntu1) groovy; urgency=medium
13
14 * Merge with Debian unstable. Remaining changes:
15diff --git a/debian/chronyd-starter.sh b/debian/chronyd-starter.sh
16index 55cc285..2539ffe 100755
17--- a/debian/chronyd-starter.sh
18+++ b/debian/chronyd-starter.sh
19@@ -25,12 +25,10 @@ fi
20
21 # Check if -x is already set manually, don't process further if that is the case
22 X_SET=0
23-while getopts ":x" opt; do
24- case $opt in
25- x)
26- X_SET=1
27- ;;
28- esac
29+for arg in $@; do
30+ if echo "$arg" | grep -q -e '^-[a-zA-Z0-9]*x'; then
31+ X_SET=1
32+ fi
33 done
34
35 if [ ${X_SET} -ne 1 ]; then

Subscribers

People subscribed via source and target branches