简体   繁体   中英

python sh library, commands with hyphen/dash

The python sh docs say:

For commands that have dashes in their names, for example /usr/bin/google-chrome, substitute the dash for an underscore:

I am trying to run the command

git rev-parse --abbrev-ref HEAD

When I try to run the command, git returns an error that I have the wrong command. Any way to get around this?

>>> from sh import git
>>> git.rev_parse('--abbrev-ref', 'HEAD')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/sh.py", line 769, in __call__
    return RunningCommand(cmd, call_args, stdin, stdout, stderr)
  File "/Library/Python/2.7/site-packages/sh.py", line 330, in __init__
    self.wait()
  File "/Library/Python/2.7/site-packages/sh.py", line 334, in wait
    self._handle_exit_code(self.process.wait())
  File "/Library/Python/2.7/site-packages/sh.py", line 348, in _handle_exit_code
    self.process.stderr
sh.ErrorReturnCode_1: 

  RAN: '/usr/bin/git rev_parse --abbrev-ref HEAD'

  STDOUT:


  STDERR:
git: 'rev_parse' is not a git command. See 'git --help'.

Did you mean this?
    rev-parse

>>> 

I am unsure as to why it's not working.

However, I found that this works instead:

git('rev-parse', '--abrev-ref', 'HEAD')

Leads to:

RAN: '/usr/bin/git rev-parse --abrev-ref HEAD'

The substitution rule is only for the command itself -- git -- not for arguments such as 'rev-parse' . This is done because dashes aren't possible in Python function names, but are perfectly possible in options.

@runDOSrun found one solution:

git('rev-parse', '--abrev-ref', 'HEAD')

That said, let's say you used subcommand syntax to pass rev-parse implicitly, like so:

git.rev_parse('--abrev-ref', 'HEAD')

The underscore would be appropriate in that case, as you would be prepending the subcommand via a Python token, limited to the usual set of characters (which excludes dashes!) available in that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM