← 输入框|github.com|刷新
Skip to content

args after -- fail #1000

Closed
Closed

Description

@pocc

Problem

Occasionally it is necessary to use -- to signify options that take an argument. When -- is used, any file arguments are ignored.

Description

I am using oclint, which identifies potential problems in c code. -- is required to specify compiler options. This is my current configuration:

[-- skipped for brevity --]

-   repo: local
    hooks:
    -   id: oclint
        name: oclint
        description: Find warnings/errors in C/CPP code
        entry: oclint
        types: [c]
        pass_filenames: true
        args: [
            -enable-clang-static-analyzer,
            -enable-global-analysis,
            --,
            -report-type=json,
            -DCMAKE_EXPORT_COMPILE_COMMANDS=ON,
        ]
        language: system

When running on a valid c file, I get this output:

oclint...................................................................Failed
hookid: oclint

oclint: Not enough positional command line arguments specified!
Must specify at least 1 positional argument: See: /usr/local/bin/oclint -help

Solution

Looking at the code, it looks like you are appending file arguments to the end instead of infixing them before --. A pull request that fixes this will be coming in a few.

Activity

  1. asottile commented on Apr 19, 2019

    @asottile
    Member

    Do you have an example that's broken? I'm not sure I understand your problem statement -- typically -- just means "don't parse these arguments as normal" and doesn't have any special significance for file or not -- I don't think it would be correct for pre-commit to attach any special behaviour to that

  2. pocc commented on Apr 19, 2019

    @pocc
    Author

    I've updated the question with additional context.

  3. asottile commented on Apr 19, 2019

    @asottile
    Member

    makes sense, I still think the current behaviour is as correct as it could be without potentially breaking existing things. oclint's usage of -- isn't universally applicable

    One thing that you can do is use a script to redirect the arguments where you want them, for instance (untested):

    entry: bash -c 'oclint -enable-clang-static-analyzer ... "$@" -- -report-type-json ...'

    (If you don't want to inline that all in the yaml, it could easily be moved out to another script)

    Even something like this might work (again untested, might be off-by-one somewhere):

    #!/usr/bin/env python3
    import os
    import sys
    
    
    def main():
        eq_eq = sys.argv.index('==')
        dash_dash = sys.argv.index('--')
        cmd = (
            ['oclint'] +
            sys.argv[1:dash_dash] +
            sys.argv[eq_eq + 1:] + 
            sys.argv[dash_dash:eq_eq]
        )
        os.execvp(cmd[0], cmd)
    
    if __name__ == '__main__':
        exit(main())
    -   repo: local
        hooks:
        -   id: oclint
            name: oclint
            description: Find warnings/errors in C/CPP code
            entry: ./oclint-pre-commit-wrapper
            types: [c]
            pass_filenames: true
            args: [
                -enable-clang-static-analyzer,
                -enable-global-analysis,
                --,
                -report-type=json,
                -DCMAKE_EXPORT_COMPILE_COMMANDS=ON,
                ==,
            ]
            language: script
  4. pocc commented on Apr 20, 2019

    @pocc
    Author

    It's a POSIX standard (see guideline 10), so it's as universally applicable as POSIX is.

    The entry syntax as shown produces the same error (no files found).
    I am using pre-commit because I had lint scripts but wanted to centralize them.

    I am making a pull request that infixes file arguments if there are options after --. I have verified that it passes all tests.

  5. asottile commented on Apr 20, 2019

    @asottile
    Member

    Yes it's a standard

    Guideline 10:
    The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the '-' character.

    No it does not say "put filename arguments before --", If anything it says to put them after as filenames could start with a -, take for instance the usage with grep:

    $ echo hi > ./-p
    $ grep h -p
    grep: invalid option -- 'p'
    Usage: grep [OPTION]... PATTERN [FILE]...
    Try 'grep --help' for more information.
    $ grep h -- -p
    hi
    
  6. asottile commented on Apr 20, 2019

    @asottile
    Member

    I fixed up the wrapper script as well, I was missing the == in the arglist that I was keying off of

  7. pocc commented on Apr 24, 2019

    @pocc
    Author

    I appreciate you taking the time to write up a solution. I'm not familiar with ==. Is this something that pre-commit will treat as "end of options"?

    In other news, I created a pull request and it passes tests that it should fail. Instead I created a pre-commit-hooks repo that contains these scripts that I'll be pushing shortly.

  8. asottile commented on Apr 24, 2019

    @asottile
    Member

    nah I just used it a delimiter so the shim script could reorder the filenames and options

  9. pocc commented on Apr 25, 2019

    @pocc
    Author

    For future reference, this will rotate args in bash:

    # Converts args array (1 3 5 7 -- 6 8 0) => (1 3 5 7 0 -- 6 8)
    # Equivalent python:  args[:args.index("--")] + [a[-1]] + args[args.index("--"):-1]
    last_idx="$(($#-1))"
    args=("$@")
    for i in $(seq 0 $last_idx); do
      if [ "${args[$i]}" == "--" ]; then
        args=("${args[@]::$i}" "${args[$last_idx]}" "${args[@]:$i:$((last_idx-i))}")
        break
      fi
    done
  10. pocc commented on Apr 25, 2019

    @pocc
    Author

    Closing as I have created https://github.com/pocc/pre-commit-hooks to deal with this instead.

  11. asottile commented on Apr 25, 2019

    @asottile
    Member

    sweet, glad we could come to a solution 🎉

  12. pocc commented on Apr 25, 2019

    @pocc
    Author

    The -- issue may pop up again, but given that it's mostly used with C/C++ compilation, my hooks will likely obviate the need for further changes.

  13. dwightgunning commented on May 2, 2019

    @dwightgunning

    I just hit the same challenge while working with Angular-cli's built-in ng lint program. Thanks for leaving the notes as they proved helpful in getting pre-commit-nglint working. 😄

  14. pocc commented on May 2, 2019

    @pocc
    Author

    Reopening this issue as this is a problem with multiple people and linter domains.

  15. reopened this on May 2, 2019
  16. pocc commented on May 2, 2019

    @pocc
    Author

    You've hardclosed this issue without a response. It sounds like you're acknowledging that this is an issue, but that

    the current behaviour is as correct as it could be without potentially breaking existing things.
    

    Would you mind adding the wontfix tag to this issue to better document it?

  17. asottile commented on May 2, 2019

    @asottile
    Member

    oops! I did type out a comment but it apparently got lost :S

    it was basically that there's nothing to standardize here and a valid way to reorder the args -- though the first comment had slightly more words 😆

    but you're correct, this is wontfix

  18. pocc commented on May 2, 2019

    @pocc
    Author

    Yeah I do that all the time. Can you add the wontfix label to this question? You use labels so effectively with other tickets - I figure one would be appropriate here too 🙂.

  19. asottile commented on May 2, 2019

    @asottile
    Member

    we used to use a [wontfix] label but found that it frustrated question askers so I removed it a while back 🤷‍♂

  20. pocc commented on May 2, 2019

    @pocc
    Author

    No problem. Thanks for the context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions