Was: Parameters of handleArgs
I went back to this conversation with Russell, and tried to use it in an
other way. I have console encoding problems with this command with Cyrillic
letters:
replace.py -catr:Венгрия . @ -lang:ru -excepttext:"[[hu:"
-save:magyarok.txt -always
One way is to urlencode the Russian category. Other way is to insert it
into a script. (DOS batch files won't work, I already tried.)
So what I did:
import replace
replace.main(u'-catr:Венгрия', '.', '@', '-lang:ru', '-excepttext:"[[hu:"',
'-save:magyarok.txt')
This results in an error message:
File "C:\Pywikipedia\replace.py", line 582, in main
for arg in pywikibot.handleArgs(*args):
File "C:\Pywikipedia\wikipedia.py", line 7795, in handleArgs
arg = _decodeArg(arg)
File "C:\Pywikipedia\wikipedia.py", line 7767, in _decodeArg
return unicode(arg, config.console_encoding)
TypeError: decoding Unicode is not supported
If I omit u from before -catr, no error is thrown, but the name is
erroneously decoded.
Now comes the tick! I went to line 7795 of current wikipedia.py (r9894) as
shown above, and commented it out. Now my script runs perfectly! I love it!
I don't want to spoil handleArgs() and I know this is an unusual use of it.
But is it possible in some way to pass a parameter to it that tells
_decodeArg to shut up? Or is there another correct way of passing Unicode
parameters from within a script?
2011/5/18 Russell Blau <russblau(a)imapmail.org>
> Bináris said:
>
> > I see in a couple of bots this construction:
> >
> > def main(*args):
> > for arg in pywikibot.handleArgs(*args):
> > etc.
> >
> > Now, if I write instead of this
> > def main():
> > for arg in pywikibot.handleArgs():
> > etc.
> > the result seems to be just the same. I tried with valid global and with
> > unique parameters as well.
> > So, what is the difference?
> > I know the theory that * means a variable width argument list, but if I
> > omit it, the behaviour does > not change.
>
> The behavior is the same if you run the script from the command line.
>
> However, using (*args) also allows you the option of running the script
> from
> inside the Python interactive interpreter; for example, if you were running
> "replace.py Foo Bar -start:!", then you could "import replace" in the
> interpreter and run <code>replace.main("Foo", "Bar", "-start:!")</code>.
> This can be useful for debugging, among other things.
>
--
Bináris